在php中检索两个表数据

时间:2015-05-30 17:22:13

标签: php mysql

我有两个表,成员(user_id,用户名,profile_picture)和证词(testimony_id,user_id,testimony_date,testimony_text)。我想在testimony.php中检索那些显示的数据(testimony_id,profile_picture,testimony_date,testimony_text)。但它不起作用,它在下面显示错误。

警告:mysql_fetch_array()期望参数1为资源,布尔值为

让我知道出了什么问题,谢谢。

testimony.php

<head>
    <title>Testimony</title>
</head>

<body>
        <table border="1px">
        <tr>
            <td>id</td>
            <td>Profile Image</td>
            <td>Username</td>
            <td>Testimony Date</td>
            <td>Testimony Text</td>
        </tr>


        <?php


            include 'action/connect.php';

            $query = "SELECT b.user_id,                             
                             a.profile_image,
                             a.username,
                             b.testimony_date,
                             b.testimony_text
                             FROM testimony te
                             JOIN member me
                             ON me.user_id = te.user_id
                             WHERE te.testimony_id = 'testimony_id'";
            $rs = mysql_query($query);
            while($row = mysql_fetch_array($rs)){
        ?>

        <tr>
            <td><?= $row['testimony_id']  ?></td>
            <td> <img src="picture/profile_image/<?=$row['profile_image']?>.jpg" width="150px" height="150px" /> </td>
            <td><?= $row['username']  ?></td>
            <td><?= $row['testimony_date']  ?></td>
            <td><?= $row['testimony_text']  ?></td>
        </tr>

        <?php
            }
        ?>
</table>
</body>

1 个答案:

答案 0 :(得分:0)

你必须在mysql_fetch_array之前检查

if($rs === FALSE) { 
    die(mysql_error()); 
}

while($row = mysql_fetch_array($rs))
{
    echo $row['username'];
}