doutun9179 2014-03-24 09:41
浏览 67
已采纳

Mysql PHP错误 - 登录验证

Good Day Gents

Busy really frustrating myself here. I am busy trying to write a simple login script that validates a login against the database.

However i keep on getting:

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

here is my code.... when i run the query on sql workbench it works 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.';
?>
  • 写回答

4条回答 默认 最新

  • dongshui2254 2014-03-24 09:42
    关注

    This is not correct

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

    better way is

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

    Need to wrap the string data in single quote.

    The POST data is directly being used in the query which is not good. Start using PDO prepared statements to avoid sql injections or at-least sanitize data as

    $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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码
  • ¥50 随机森林与房贷信用风险模型
  • ¥50 buildozer打包kivy app失败
  • ¥30 在vs2022里运行python代码
  • ¥15 不同尺寸货物如何寻找合适的包装箱型谱
  • ¥15 求解 yolo算法问题