Paul
Paul

Reputation: 3368

Matching id's from two database tables to get username from one

I am wanting to match my user_id column from my announcements table to the id column in my users table. I then want to get the username from the users table where the id's match.

I initially had the following query

if ($announcements_stmt = $con->prepare("SELECT * FROM announcements"))

I am getting the following error with my current code..

Warning: mysqli_stmt::bind_result(): Number of bind variables doesn't match number of fields in prepared statement in  

Which I know what this means, but do I need to add in every column table from my users table for this to work or is there another way to do this? If I do need to add all of the columns as variables in my bind_result, does it matter which order I put them in? Announcements first or users or vise versa?

if ($announcements_stmt = $con->prepare("SELECT * FROM announcements
                        INNER JOIN users
                        ON announcements.user_id = users.id")) {


    $announcements_stmt->execute();
    $announcements_stmt->bind_result($announcements_id, 

$announcements_user_id, $announcements_messages, $announcements_date); 

        if (!$announcements_stmt) {
            throw new Exception($con->error);
        }
        $announcements_stmt->store_result();
         $announcements_result = array();

?>

            Current Announcements
            <table>
                <tr>
                    <th>ID</th>
                    <th>Username</th>
                    <th>Message</th>
                    <th>Date</th>
                </tr>   
<?php
        while ($row = $announcements_stmt->fetch()) {
?>
                <tr>
                    <td><?php echo $announcements_id; ?></td>
                    <td><?php echo $announcements_username; ?></td>
                    <td><?php echo $announcements_messages; ?></td>
                    <td><?php echo $announcements_date; ?></td>
                </tr>   

<?php
        } 
?>

    }

update..

 if ($announcements_stmt = $con->prepare("SELECT announcements.id, announcements.user_id, announcements.messages, announcements.date, users.username FROM announcements
                        INNER JOIN users
                        ON announcements.user_id = users.id")) {


    $announcements_stmt->execute();
    $announcements_stmt->bind_result($announcements_id, 

$announcements_user_id, $announcements_messages, $announcements_date, $announcements_username); 

        if (!$announcements_stmt) {
            throw new Exception($con->error);
        }
        $announcements_stmt->store_result();
         $announcements_result = array();

?>

            Current Announcements
            <table>
                <tr>
                    <th>ID</th>
                    <th>Username</th>
                    <th>Message</th>
                    <th>Date</th>
                </tr>   
    <?php
            while ($row = $announcements_stmt->fetch()) {
    ?>
                    <tr>
                        <td><?php echo $announcements_id; ?></td>
                        <td><?php echo $announcements_username; ?></td>
                        <td><?php echo $announcements_messages; ?></td>
                        <td><?php echo $announcements_date; ?></td>
                    </tr>   

    <?php
            } 
    ?>

        }
                </table>
    <?php 
            }           
        }

Upvotes: 1

Views: 354

Answers (1)

Devon Bessemer
Devon Bessemer

Reputation: 35337

The warning indicates when you are binding the result fields into variables, the number of variables does not match the number of fields in the result set:

$announcements_stmt->bind_result($announcements_id, $announcements_user_id, $announcements_messages, $announcements_date, $announcements_username); 

The easy way around this is to always specify the fields in the SELECT statement (just an example):

SELECT t1.id, t1.user_id, t1.messages, t1.date, t2.username

Instead of:

SELECT *

Upvotes: 1

Related Questions