Fetching data from mutiple table from the database

this is the code but its just not working, its suppose to pick two users from my ‘paired_members’ table and pick the ‘donation’ from my ‘account’ table

<?php
 $query="select * from members";
$query_run =  mysqli_query($connect,$query);
$nume = mysqli_num_rows($query_run);
                    
      $query=" SELECT * FROM paired_members a left join account b  on (a.user_id_1 = b.user_id  )
      left join members c on (a.user_id_1 = c.id  ) where b.confirmation = 0  limit $eu, $limit ";


                           $result = mysqli_query($connect,$query);
                            echo   mysqli_error($connect);


                           while($row = mysqli_fetch_assoc($result)){
                             $username = $row['name'];
                                $donation = $row['initial_payment'];
                                 $withdraw = $row['withdraw'];
                                  $account_number = $row['account_number'];
                                   $phone = $row['phone'];


?>

Sorry, but “just not working” isn’t enough information.
What error messages are you getting if any?
What is it returning that is not what you want it to return?

1 Like

there are no error message, it is not returning the amounts and the second user. and its no fetching the column from the account table

here are my tables

CREATE TABLE IF NOT EXISTS paired_members (
id int(11) NOT NULL,
user_id_1 int(11) NOT NULL,
user_id_2 int(11) NOT NULL,
duration varchar(10) NOT NULL,
user_1_donation double NOT NULL,
user_2_donation double NOT NULL,
date datetime NOT NULL DEFAULT CURRENT_TIMESTAMP
)

CREATE TABLE IF NOT EXISTS account (
id int(11) NOT NULL,
confirmation int(11) NOT NULL,
initial_payment varchar(200) NOT NULL,
datetime datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
on_current_date datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
withdraw varchar(200) NOT NULL,
user_id int(11) NOT NULL,
withdrawal varchar(200) NOT NULL
)

Are you sure that
on (a.user_id_1 = c.id ) shouldn’t be c.user_id ?

Are $eu and $limit what you think they are?

NO, have tried it ,but still not working. it not inserting to the paired_members table.

Well, I think you’ll need to get to where both of those are Yes before you can proceed further.
If you have forgotten what the members id field is, you should check (hopefully you left comments in your code somewhere) .
If $eu / $limit are not what they should be you’ll need to fix wherever the values are coming from. Though I guess for now you could also try removing the LIMIT from the query and see if you get any results without it.

Unless you have something going on somewhere else, that SELECT will not do an INSERT

+1 for the “just not working” comment.

Another problem that I can clearly see from just the snippet, there’s a missing curly brace where it’s supposed to end the while loop.


Another issue I see is SQL Injections. You need to use prepared statements where necessary and even if it isn’t, it’s good practice to know that you are using modern practices for using up-to-date code. I highly recommend using prepared statements to lessen the load on SQL Injections because you are wide open for it when you are taking in user input like you are in your second SQL query.

I might be reading too much into this, but where I can see a left join in the main query to link the members and account tables based on a.user_id_1, I can’t see anything that would link to those tables for the second paired member, which is presumably stored in a.user_id_2 in the paired_members table. Can you var_dump($row) inside that loop and see if anything shows up?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.