用PHP从sql数据库中检索数据时遇到问题

时间:2013-11-06 21:39:44

标签: php sql

我对php很新,而且我在教自己。我看了几个不同的资源,我现在的PHP脚本在执行时没有返回任何严重错误,但它没有从表中返回数据。

 <?php

$connect = mysqli_connect("localhost","*","*","*");

if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$comments = "SELECT * FROM commentstable";

$rs = mysqli_query($connect,$comments);

$fetch = mysqli_fetch_array($rs);

while($fetch = mysqli_fetch_array($rs)) {
    echo $fetch['comments'];

    }
echo $fetch;

mysqli_close($connect);

echo "hello";

?>

2 个答案:

答案 0 :(得分:1)

你有双重进入:

$fetch = mysqli_fetch_array($rs); //<--- remove this as you are calling it again in the while loop

while($fetch = mysqli_fetch_array($rs)) {    
    echo $fetch['comments'];

}

答案 1 :(得分:0)

检查

$connect = mysqli_connect("localhost","turlough","samus1","comments");
if (mysqli_connect_errno())
{
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else
{
    $comments = "SELECT * FROM commentstable";

    $rs = mysqli_query($connect,$comments);
    if($rs)
    {
        while($fetch = mysqli_fetch_array($rs)) {
            echo $fetch['comments'];
        }
    }
    else
    {
        // no results from query
    }

    mysqli_close($connect);

}