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:

<?php
include_once 'php/connection.php';

        $sess = $_SESSION['email'];

        $sql="SELECT firstname, lastname, email, phone, birthday, gender FROM users WHERE email = ?";
        $stmt = $conn->prepare($sql);
        $stmt->bind_param("s", $sess);
        $stmt->execute();
        

        if ($stmt->error){echo "something has gone wrong";}
        $result=$stmt->get_result();                                 
        while($row = mysqli_fetch_array ( $result, MYSQLI_ASSOC )) {
?>

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

<h2><?php echo $row["firstname"];?></h2>
<?php 
     }
     $conn->close();
?>

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:

<?php
include_once 'php/connection.php';

        $sess = $_SESSION['email'];

        $sql="SELECT firstname, lastname, email, phone, birthday, gender 
              FROM users, booking
              WHERE users.email = booking.email AND email = ?";
              $stmt = $conn->prepare($sql);
              $stmt->bind_param("s", $sess);
              $stmt->execute();
        

            if ($stmt->error){echo "something has gone wrong";}
            $result=$stmt->get_result();                                 
            while($row = mysqli_fetch_array ( $result, MYSQLI_ASSOC )) {
?>

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.

SELECT u.firstname, u.lastname, 
       u.email, u.phone, u.birthday, u.gender,
       b.something 
  FROM users u
  LEFT JOIN booking b ON u.email = b.email
 WHERE u.email = ?

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.

        $stmt = $conn->prepare($sql);
        if ( !$stmt ) {
           $message = $conn->error;
           /* statement didn't prepare correctly */
        }
        $stmt->bind_param("s", $sess);
        $stmt->execute();
        if ($stmt->error){echo "something has gone wrong";}
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement