MySQL结果集不提取数据

时间:2013-11-09 11:06:59

标签: php mysql

我使用sql查询从Mysql db获取数据。

$query = "select * from User u";
$result = mysqli_query($dbConnect, $query);
while ($row = mysql_fetch_array($result)) {
    echo $row['firstName'];
    echo $row['lastName'];
    echo $row['city'];
    echo $row['state'];
    echo $row['zipcode'];
    echo $row['location'];
    echo $row['phoneNumber'];
}
mysqli_close($dbConnect);
?>

以上代码获取数据并在浏览器中显示。但是,当我尝试使用此数据创建表时,它会因以下代码而失败:

    $query = "select * from User u";
$result = mysqli_query($dbConnect, $query);
while ($row = mysql_fetch_array($result)) {
    ?>
    <tr>
        <td><?php $row['firstName']?></td>
        <td><?php $row['location']?></td>
        <td><?php $row['city']?></td>
        <td><?php $row['state']?></td>
        <td><?php $row['zipcode']?></td>
        <td><?php $row['phoneNumber']?></td>
    </tr>
    <?php
    mysqli_close($dbConnect);
}
?>

我收到错误mysql_fetch_array()期望参数1是资源,在此处选择布尔值。

更新:(在Rockstar社区成员的投入之后)

<?php
    include('../include/db_connection.php');
    $query = "select * from User u";

    $result = mysqli_query($dbConnect, $query);
    while ($row = mysql_fetch_array($result)) {
?>
<tr>
    <td><?php echo $row['firstName'];?></td>
    <td><?php echo $row['location'];?></td>
    <td><?php echo $row['city'];?></td>
    <td><?php echo $row['state'];?></td>
    <td><?php echo $row['zipcode'];?></td>
    <td><?php echo $row['phoneNumber'];?></td>
</tr>
<?php
        mysqli_close($dbConnect);
    }
?>

2 个答案:

答案 0 :(得分:2)

更正您的获取代码

while ($row = mysql_fetch_array($result)) {

while ($row = mysqli_fetch_array($result)) {

使用mysql或mysqli,我觉得你应该首选mysqli。不要混合它们。

答案 1 :(得分:0)

我认为您正在使用mysqli_close($dbConnect);〓在while内 这可能导致...停止提取数据