php - 登录脚本中的 "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given"

标签 php mysql function

在这里忙得让自己很沮丧。我正忙着尝试编写一个简单的登录脚本来验证对数据库的登录。

但是我不断得到:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

这是我的代码....当我在 sql workbench 上运行查询时,它 100% 有效

<?php

// Grab User submitted information
$email = $_POST['users_email'];
$pass = $_POST['users_pass'];

// Connect to the database
$con = mysql_connect('localhost','root','');
// Make sure we connected succesfully
if(! $con)
{
    die('Connection Failed'.mysql_error());
}

// Select the database to use
mysql_select_db('arctecs',$con);

$result = mysql_query('SELECT users_email, users_pass FROM users WHERE users_email = $email');

$row = mysql_fetch_array($result);

if($row['users_email']==$email && $row['users_pass']==$pass)
    echo'You are a validated user.';
else
    echo'Sorry, your credentials are not valid, Please try again.';
?>

最佳答案

这是不正确的

'SELECT users_email, users_pass FROM users WHERE users_email = $email'

更好的方法是

"SELECT users_email, users_pass FROM users WHERE users_email = '$email'"

需要将字符串数据用单引号括起来。

在查询中直接使用 POST 数据是不好的。开始使用 PDO 准备语句来避免 sql 注入(inject)或至少将数据清理为

$email = $_POST['users_email'];
$pass = $_POST['users_pass'];
$con = mysql_connect('localhost','root','');
// Make sure we connected succesfully
if(! $con)
{
    die('Connection Failed'.mysql_error());
}

$email = mysql_real_escape_string($email);

关于php - 登录脚本中的 "Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22605672/

相关文章:

PHP - MySQL "next"- "previous"链接与订购

php - 在这种情况下是使用接口(interface)还是抽象类更好?

php - 我怎样才能加快这个查询?

string - 关于Matlab的问题: passing strings as split parameters to function

php - mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows 等...期望参数 1 是资源

php - INSERT INTO TBL_payments undefined index 和父子外键

Mysql 归档旧数据

php - 我无法使用 mysql 通过内连接从数据库中选择数据

python - 带有 *args 函数的装饰器

bash - 将字符串作为带空格的参数传递给 bash 函数