php 警告:mysqli_fetch_array() 期望参数 1 是 mysqli_result,第 6 行给出的对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34355128/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 23:55:51  来源:igfitidea点击:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given on line 6

phpmysqli

提问by Hello World

<?php 
if (isset($_GET['hash'])&&!empty($_GET['hash'])){
        $hash = $_GET['hash'];
        $message_query = "SELECT from_id, message FROM message WHERE hash='$hash'";
        $run_messages = mysqli_query($con,$message_query);
        while($row_messages = mysqli_fetch_array($con,$run_messages)){
             $form_id = $row_messages['from_id'];
             $message = $row_messages['message'];

             $user_query = "SELECT username FROM admins WHERE id='$from_id'";
             $run_user = mysqli_fetch_array($con,$user_query);
             $from_username = $run_user['username'];

             echo "<p><strong>$from_username</strong></p></br>";
        }
}else{
    header('Location: messages.php');
}
?>

I'm getting this error message:

我收到此错误消息:

Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, object given on line 6

警告:mysqli_fetch_array() 期望参数 1 是 mysqli_result,第 6 行给出的对象

Here's line 6 & as you can see I have already included the $conwhich is my database connection.

这是第 6 行 & 正如您所看到的,我已经包含了作为我的数据库连接的$con

while($row_messages = mysqli_fetch_array($con,$run_messages)){

while($row_messages = mysqli_fetch_array($con,$run_messages)){

采纳答案by Saty

mysqli_fetch_array

mysqli_fetch_array

mysqli_fetch_array ( mysqli_result $result [, int $resulttype = MYSQLI_BOTH ] )

mysqli_fetch_array ( mysqli_result $result [, int $resulttype = MYSQLI_BOTH ] )

First parameter is Specifies a result set identifier returned by mysqli_query()and second parameter is result type

第一个参数是Specifies a result set identifier returned by mysqli_query(),第二个参数是result type

Remove connection as first parameter

删除连接作为第一个参数

It would be

这将是

$row_messages = mysqli_query($run_messages,MYSQLI_ASSOC);

Your code is open for sql injection better use bind statement

你的代码是开放的 sql 注入更好地使用绑定语句

http://php.net/manual/en/mysqli-stmt.bind-param.php

http://php.net/manual/en/mysqli-stmt.bind-param.php

To check error in your connection and query use

要检查连接和查询中的错误,请使用

/* check connection */

if ($mysqli->connect_errno) {
    printf("Connect failed: %s\n", $mysqli->connect_error);
    exit();
}

if (!$mysqli->query("SET a=1")) {
    printf("Errormessage: %s\n", $mysqli->error);
}

http://php.net/manual/en/mysqli.error.php

http://php.net/manual/en/mysqli.error.php

In second query you use

在您使用的第二个查询中

$user_query = "SELECT username FROM admins WHERE id='$from_id'";

$user_query = "SELECT username FROM admins WHERE id='$from_id'";

TYPO here

打错字在这里

$form_id !=$from_id

Change this to

将此更改为

$user_query = "SELECT username FROM admins WHERE id=' $form_id'";

$user_query = "SELECT username FROM admins WHERE id=' $form_id'";

回答by Matt

<?php 
if (isset($_GET['hash'])&&!empty($_GET['hash'])){
        $hash = mysqli_escape_string($con, $_GET['hash']);
        $message_query = "SELECT from_id, message FROM message WHERE hash='$hash'";
        $run_messages = mysqli_query($con,$message_query);
        while($row_messages = mysqli_fetch_array($run_messages, MYSQLI_ASSOC)){
             $from_id = $row_messages['from_id'];
             $message = $row_messages['message'];

             $user_query = "SELECT username FROM admins WHERE id='$from_id'";
             $query_run = mysqli_query($con, $user_query);
             $run_user = mysqli_fetch_array($query_run, MYSQLI_ASSOC);
             $from_username = $run_user['username'];

             echo "<p><strong>$from_username</strong></p></br>";
        }
}else{
    header('Location: messages.php');
}
?>

Essentially mysqli_fetch_array has one required parameter which is the result of a queryand an optional query of the result type. $run_messagesis the result of the first query and will be used for the execution of mysql_fetch_arrayand MYSQLI_ASSOCis the optional type that you will be using so you can access the values in $row_messages like you do.

本质上 mysqli_fetch_array 有一个必需参数,它是查询结果和结果类型的可选查询。 $run_messages是第一个查询的结果,将用于执行mysql_fetch_arrayMYSQLI_ASSOC是您将使用的可选类型,因此您可以像访问 $row_messages 一样访问值。

Read mysqli_fetch_arrayfor more information.

阅读mysqli_fetch_array了解更多信息。

Also, please remember to escape user valued with mysqli_escape_string before allowing the data to be queried into the database. This reduces the chance of SQLi.

另外,请记住在允许将数据查询到数据库之前使用 mysqli_escape_string 转义用户值。这减少了 SQLi 的机会。