This question already has answers here:
mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource or result
                                
                                    (32个答案)
                                
                        
                        
                            mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in [duplicate]
                                
                                    (1个答案)
                                
                        
                                3年前关闭。
            
                    
下面是代码:

<?php

$conexion = mysqli_connect('localhost', 'root', '', 'ingeniera_de_software_db');

$user = $_POST['usuario'];
$pass = $_POST['contrasena'];

$check = "SELECT count(*) AS A FROM usuarios WHERE usuario = '" . $user . "' AND contraseña = md5('" . $pass . "');";
$resultado = mysqli_query($conexion, $check);

$r = mysqli_fetch_array($resultado);

echo $r['A'];

 ?>


但是,执行后,它不会像预期的那样返回1,而是返回:


  警告:mysqli_fetch_array()期望参数1为mysqli_result,
  布尔值


我试图将查询直接放在sql编辑器中,并且它可以正常工作,我使用这种类型的查询没有任何问题。

顺便说一句,我知道这并不安全,但我无法通过这一点。

最佳答案

试试下面的代码。还要确保在mysqli_connect中输入了正确的数据库详细信息。

<?php
$conexion = mysqli_connect('localhost', 'root', '', 'ingeniera_de_software_db');

if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();die;
}

$user = $_POST['usuario'];
$pass = $_POST['contrasena'];
$check = "SELECT count(*) AS A FROM usuarios WHERE usuario = '" . $user . "' AND contraseña = md5('" . $pass . "');";
$resultado = mysqli_query($conexion, $check);
$r = mysqli_fetch_array($resultado,MYSQLI_ASSOC);
print_r($r);
?>

关于php - 带有PHP的SQL mysqli_fetch_array错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37536609/

10-09 00:36