简体   繁体   中英

SQL inner join getting all data in column

This is my first time using an inner join so i'm very confused.

I have two tables.

第一个:(我已将详细信息舍去)

This is my first table called members

在此处输入图片说明

This is my other table called donations. The userID from the members is linked up with the userID on the donations table.

Right so what i'm trying to do is select all of the data from members and from the donations table and assiotate each Id with the donation amount. So what i'm trying to do is echo all of the names along side their donation amount if that makes sense.

This is my code at the moment

$connect - contains my config

  //Connection info.
  global $connect;


  //inner join
  $sql = "SELECT members.firstname, members.lastname 
  FROM members INNER JOIN   donations ON members.userID = donations.userID WHERE donations.amount !='' ORDER BY members.userID ASC ";

  $result = mysqli_query( $connect, $sql);


  while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){

    $list .= $row["firstname"];
    echo $list;
}

I'm getting this error back: mysqli_fetch_array() expects parameter 1 to be mysqli_result boolean

UPDATE: Thanks for all your help, i'm running the SQL query and just getting the first and last name back.

  SELECT members.firstname, members.lastname FROM members INNER JOIN   donations ON members.userID = donations.userID WHERE donations.amount !='' ORDER BY members.userID ASC

I think i'm doing something wrong here !='' the donation amount is a decimal am i targeting it right?

You have typo in column name 'fisrname' => 'firstname'. Check the query first in phpmyadmin or the other tool. Also read about mysqli_error and later about other means of accessing the DB (like PDO, Doctrine etc.).

Replace members.id by members.userID in your query.

SELECT members.firstname, members.lastname, donations.amount 
FROM members
INNER JOIN donations ON members.userID = donations.userID
WHERE donations.amount != ''
ORDER BY members.userID ASC

As for the SQL error, it's because $result is false when there's a problem with the query or no result has been found.

mysqli_fetch_array() expects parameter 1 to be mysqli_result boolean

To prevent the error, add a simple if case because your while.

if($result){
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
       $list .= $row["firstname"];
       echo $list;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM