簡體   English   中英

從兩個數據庫表中匹配ID以從一個數據庫中獲取用戶名

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

我想將我的公告表中的user_id列與用戶表中的id列進行匹配。 然后,我想從id匹配的users表中獲取用戶名。

我最初有以下查詢

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

我的當前代碼出現以下錯誤。

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

我知道這意味着什么,但是我需要在我的用戶表中的每個列表中添加此功能,還是有另一種方法來做到這一點? 如果確實需要將所有列都添加為我的bind_result中的變量,那么將它們按什么順序排列是否重要? 公告是先發布還是用戶發布,反之亦然?

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

    }

更新..

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

該警告指示您將結果字段綁定到變量中時,變量數與結果集中的字段數不匹配:

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

解決此問題的簡單方法是始終在SELECT語句中指定字段(僅作為示例):

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

代替:

SELECT *

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM