【问题标题】:error mysqli_fetch_array() expects parameter 1 to be mysqli_result, string given错误 mysqli_fetch_array() 期望参数 1 为 mysqli_result,给定字符串
【发布时间】:2015-10-09 07:20:15
【问题描述】:

警告: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'>

【问题讨论】:

  • 您应该将mysqli_query() 的_result_ 传递给mysqli_fetch_array(),而不是查询本身...

标签: php sql mysqli


【解决方案1】:

问题

您缺少如何将参数传递给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))) {}(但不建议这样做,只是为了向您展示它是如何工作的)。

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

      你可以这样写:-

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-03-12
        • 2015-06-21
        • 1970-01-01
        • 2023-03-13
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-03-04
        相关资源
        最近更新 更多