SQL inner join getting all data in column

Ryan

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

I have two tables.

First one it: (I've blanked out details)

This is my first table called members

enter image description here

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?

Chin Leung

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;
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Getting all weeks data of a month using inner join

getting count on same column using inner join

Getting error in SQL query using INNER JOIN

INNER JOIN with ON All columns except one Column

SQL Query - Getting Sum Of Multiple Column with Id in Table Using Inner Join

SQL Adding a Column to table, based on a inner join

Getting error when I'm trying to get data from 3 table using sql INNER JOIN query

Get all siblings with sql inner join

SQL QUERY Inner Join missing data

Use INNER JOIN for update sql data

SQL INNER JOIN and AVG() returning wrong data

SQL INNER JOIN - Column name in join is also in select *error*

Group By column on SQL Server and concat inner join to that grouped column

SQL Server inner join only getting results from 1 table

Getting 4 rows instead of 2 in a simple inner join in sql developer

INNER JOIN with a COUNTER COLUMN?

SQL INNER JOIN Concatenated Field with a column in another table

SQL Insert with Inner Join trying to insert into wrong column

SQL Multiple values from same column with inner join

Max with inner join need more column in SQL Server

SQL sort by max Column and Inner join multiple tables

How can we Group BY a column in SQL with inner join?

SQL INNER JOIN / Unknown column C.book in On Clause

How to Select a column in the inner join without group by SQL

Inner Join data.table without unwanted column(s)

Inner join of two data frames still showing all values

SQL Row Data to Column Data in Join

Find all records with same join column data

SQL JOIN two tables: inner join on one column and left join on another