知道为什么这个while循环不起作用?

时间:2013-09-08 05:59:02

标签: php mysqli while-loop

<?php 
$username = 'Gianna';
$con = mysqli_connect("$db_host","$db_username","$db_pass","$db_name");
if (!$con){die('Could not connect: ' .mysql_error());}
$result = mysqli_query($con,"SELECT * FROM $s_table WHERE stName='$username'");
    while ($row = mysqli_fetch_array($result)){
        $data1 = "instructor=";
        $data2 = $data1."'".$row['insName']."'".",";
        $trimmed = rtrim($data2, ",");}
mysqli_close($con);
?>

我已经成功运行了许多其他脚本,但由于某种原因它不断返回PHP Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given on line 56第56行是while循环。

3 个答案:

答案 0 :(得分:1)

您的查询可能失败,因此mysqli_query将$ result设置为FALSE(boolean)。这使您在mysqli_fetch中出现类型不匹配。

答案 1 :(得分:1)

当查询变为false时,就会出现此警告,因此请通过添加die()

来检查您的查询

检查

$result = mysqli_query($con,"SELECT * FROM $s_table WHERE stName='$username'") or die("some problem in query");

您也可以使用mysqli_error()

答案 2 :(得分:-1)

更改     $ result = mysqli_query($ con,“SELECT * FROM $ s_table WHERE stName ='$ username'”);

要     $ result = mysqli_query(“SELECT * FROM $ s_table WHERE stName ='$ username'”,$ con);

看到差异?

此外,$ s_table可能没有按预期正确格式化。尝试使用连接它 string.variable.string style