php 错误:mysqli_fetch_array() 期望参数 1 为 mysqli_result,给出布尔值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21463026/
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 04:10:19  来源:igfitidea点击:

ERROR: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given

phpmysqli

提问by user3251639

getting this error in login, I have installed new version of xampp/php still facing lots of problems for mysqli...

在登录时遇到这个错误,我已经安装了新版本的 xampp/php 仍然面临着 mysqli 的很多问题......

below is code for login.php after that it goes to lock.php the code is below...

下面是 login.php 的代码,然后它转到 lock.php 代码如下...

<?php

include("config.php");
session_start();

if($_SERVER["REQUEST_METHOD"] == "POST")
{
// username and password sent from form 

$myusername=addslashes($_POST['username']); 
$mypassword=addslashes($_POST['password']); 


$sql="SELECT id FROM mc_admin WHERE username='$myusername' and passcode='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];

$count=mysql_num_rows($result);


// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1)
{
session_register("myusername");
$_SESSION['login_user']=$myusername;

header("location: home.php");
}
else 
{
$error="Your Login Name or Password is invalid";
}
}
?>

code for lock.php below:

下面是 lock.php 的代码:

<?php
include('config.php');
session_start();
$user_check=$_SESSION['login_user'];

$ses_sql=mysql_query("select username from mc_admin where username='$user_check' ");

$row=mysql_fetch_array($ses_sql);

$login_session=$row['username'];

if(!isset($login_session))
{
header("Location: login.php");
}
?>

回答by Nouphal.M

MYSQL query returns FALSE on error, So one of your queries has an error while executing.

MYSQL 查询在出错时返回 FALSE,因此您的查询之一在执行时出错。

As per PHP docs mysql_query() returns a resource on success, or FALSE on errorSo before trying to call the mysql_fetch_arraycheck as below:

根据 PHP 文档mysql_query() returns a resource on success, or FALSE on error,在尝试调用如下mysql_fetch_array检查之前:

You can print the error

您可以打印错误

$result = mysql_query('your query');
if (!$result) {
    die('Invalid query: ' . mysql_error());
}

Also you must note that now mysql_* functions are deprecated as of PHP5.5. INstead of mysql_* you could use mysqlior PDO

您还必须注意,从 PHP5.5 开始,mysql_* 函数已被弃用。您可以使用mysqliPDO而不是 mysql_*

回答by mboy

Move to mysqli! Check this how I solved mine. I'm getting the same error when the query is INSERT because it returns empty so I only call fetch_assoc()when needed.

转移到mysqli!检查这个我是如何解决我的。当查询是 INSERT 时,我遇到了同样的错误,因为它返回空,所以我只fetch_assoc()在需要时调用。

mysqlcon.php

mysqlcon.php

<?php

function mysqlquery($query,$dbcon,$returnValue = false){
  $result = $dbcon->query($query);

  if(!$result){
    die('There was an error running the query [' . $dbcon->error . ']');
  }

  //Do not use `fetch_assoc()` in INSERT query to prevent
  //`PHP Fatal error:  Call to a member function fetch_assoc() on a non-object` error.
  if($returnValue){
    return $result->fetch_assoc();
  }
}


function dbcon(){

  $db_address = 'localhost';
  $db_user = 'root';
  $passwd = 'your_password';
  $db = 'your_db';

  $dbcon = new mysqli($db_address, $db_user, $passwd, $db);
  if($dbcon->connect_errno > 0){
    die('Unable to connect to database [' . $dbcon->connect_error . ']');
  }

  return $dbcon;
}

Example usage:

用法示例:

exaple.php

例子.php

<?php
require_once('mysqlcon.php');

$dbcon = dbcon();

$result = mysqlquery("SELECT * FROM TABLE WHERE COLUMN='hello'",$dbcon,true);
echo $res = $result['db_column'];