错误mysqli_fetch_array()期望参数1为mysqli_result,给定字符串

时间:2015-07-20 08:30:20

标签: php sql mysqli

  

警告:mysqli_fetch_array()要求参数1为mysqli_result,给定字符串

这是我的代码可以有人告诉我有什么问题吗?

$result ="SELECT * FROM report" ;
if(mysqli_query($cons, $result)) {
echo("
<div class='sc'>
<table  id='to1' width='90%' border='0' cellspaceing='1' cellpadding='8' align='center'>
 <tr bgcolor='#9B7272'>
<td > ID </td>
<td > first_name </td>
<td > last_name </td>
<td > phone </td>
<td > address </td>
<td > email </td>
<td > birthdate </td>
<td > gender </td>
<td > city </td>
<td > dr_name </td>

</tr>

");
while($row = mysqli_fetch_array($result))
{
$ID         =$row['ID']; 
$first_name =$row['first_name'];
$last_name  =$row['last_name'];
$phone      =$row['phone'];
$address    =$row['address'];
$email      =$row['email'];
$birthdate  =$row['birthdate'];
$gender     =$row['gender'];
$city       =$row['city'];
$dr_name    =$row['dr_name'];

echo "  <tr bgcolor='#C7B8B8'>

3 个答案:

答案 0 :(得分:3)

<强>问题

您错过了如何将参数传递给mysqli_fetch_array()

<强>解决方案

因此,这一行:

if(mysqli_query($cons, $result)) {

应该是

if($res = mysqli_query($cons, $result)) { // assign the return value of mysqli_query to $res

(FWIW,我跟$res = mysqli_query($cons, $result);一起去做if($res) {。)

然后再做

while($row = mysqli_fetch_array($res)) // pass $res to mysqli_fetch_array instead of the query itself

<强>为什么吗

您正在向mysqli_fetch_array() - 作为参数 - 提供包含您的查询的string。这不是它的工作原理。您应该传递mysqli_query()的返回值。因此,你也可以写:while($row = mysqli_fetch_array(mysqli_query($cons, $result))) {}(但不建议,只是为了告诉你它是如何工作的)。

答案 1 :(得分:1)

如评论中所述,您需要从查询中设置$result,然后在循环中使用它。

$qry ="SELECT * FROM report";
$result = mysqli_query($cons, $qry);
if ($result){
    while($row = mysqli_fetch_array($result)){
    }
}

答案 2 :(得分:1)

你可以这样写: -

$query = mysqli_query($cons,"SELECT * FROM items WHERE id = '$id'");
if (mysqli_num_rows($query) > 0)
{
while($row = mysqli_fetch_assoc($query))
{
  $result=$row;
}
}