Skip to content
Advertisement

Data from 2 tables is not displaying on the same page

I’m a newbie in PHP and mySQL, I’m currently working on a profile page where I will display the logged in user’s personal information from one table called

users

and also display the tours that they will book in the future from my website from this table:

booking

I’m fetching the data from users table with prepared statement which will only select the data of logged in user without any problems, here is the code:

JavaScript

I close the connection after the section where the data will be displayed with code:

JavaScript
JavaScript

However, I face the problem when I try to fetch data from both tables and display it on the page, I’ve done research and tried many options but it seems to be not working, here is what I’ve tried:

JavaScript

by using this script it’s showing me this error:

Fatal error: Uncaught Error: Call to a member function bind_param() on boolean in /storage/ssd1/136/16588136/public_html/profile.php:82 Stack trace: #0 {main} thrown in /storage/ssd1/136/16588136/public_html/profile.php on line 82

I’ve been trying to fix this problem all day but I can’t come up with a solution. Could someone please help me out on this? Thanks in advance.

Advertisement

Answer

You’re very close. Your ->prepare() call is failing due to an error in your SQL, so it returns a $stmt value of false rather than a real statement object.

When you JOIN multiple tables you need to qualify the column names in your query with table names.

JavaScript

Notice this uses LEFT JOIN syntax instead of the comma-join syntax in your example. Comma-join syntax became obsolete three decades ago. (Yeah, I know, time flies.)

And notice it uses table aliases u and b for the tables, to shorten the query.

And, you should check errors after each database function call. That way you can catch this sort of problem more easily.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement