我对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";
?>
答案 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);
}