douguang9014 2016-10-04 00:57
浏览 44

无法从sql数据库获取结果[重复]

I am very new to php and I am trying to create a login system. Here is my code

<?php

    $con=mysqli_connect("XXXXXXX","XXXXXXXX","XXXXXXXXX","XXXXXXXXXX");

    if (!$con)
        {
        echo "failed to connect";
        }

    $username = $_POST['username'];
    $password = $_POST['password'];

    $sql = "SELECT userID FROM users WHERE username = $username and password =          $password;";

    if (!$sql) {
        echo 'query invalid'.mysql_error();
        }

    $result = mysql_query($sql);

    echo "$result";

    $row = mysqli_fetch_array($sql, MYSQLI_ASSOC);

    $active = $row['active'];

    $count = mysqli_num_rows($result);

    mysql_close($con);

?>

I am sure there is no problem with the connection. But my result is not showing up and there is no error message. Previously I had an IF statement that perform further action if the result comes back. Since I am trying to figure out what is going on, I just deleted that part. Somebody please help. Many thanks

</div>
  • 写回答

2条回答 默认 最新

  • douluokuang7184 2016-10-04 01:00
    关注

    You're missing the single quotes around the variables, and also you're using mysql mixed with mysqli, which will not work.

    $sql = "SELECT userID FROM users WHERE username = '$username' and password = '$password';";
    $result = mysqli_query($con, $sql);
    
    $row = mysqli_fetch_assoc($sql); // same as fetch_array with MYSQLI_ASSOC
    
    $active = $row['active'];
    
    $count = mysqli_num_rows($result);
    

    You don't need mysql_close at the end, but if you want to use it, it's mysqli_close($con); Keep in mind this is unsafe.

    Use this function to filter user input:

    $username = mysqli_real_escape_string($con, $_POST['username']);
    $password = mysqli_real_escape_string($con, $_POST['password']);
    

    Read this to make sure your code follows the security standards.

    评论

报告相同问题?

悬赏问题

  • ¥50 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 latex怎么处理论文引理引用参考文献
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?