简体   繁体   English

使用php从sql数据库检索数据时遇到麻烦

[英]trouble retrieving data from sql database with php

I'm pretty new to php, and I'm teaching myself. 我对php很陌生,而且我正在自学。 I've looked at a few different resources, and the php script I have now doesn't return any critical errors when executed, but its not returning the data from the table. 我查看了一些不同的资源,并且我现在拥有的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";

?>

you have double entry: 您有两次输入:

$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'];

}

Check this 检查一下

$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);

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM