dtncv04228 2018-08-25 09:04
浏览 262

mysqli_fetch_array($ result,MYSQLI_NUM);

This mysqli_fetch_array not returning values. Checked it for SQL errors, none found. some error in PHP or functions I guess. Please help me correct it.

$login = login($username, $password);
if ($login == false) {
    $errors[] = "That username and password     combination is incorrect";
}

// validate login
function login($username, $password){
    $user_id = id_from_username($username);
    echo $user_id;
    $password = md5($password);
    $username = sanitize($username);
    $query = "SELECT `password` FROM `user_data` WHERE `username` = '$username' ";
    $result = mysqli_query($conn,$query);
    $row = mysqli_fetch_array($result, MYSQLI_NUM);

    if ($row[0] == $password) {
        echo "Login successful";
        return $user_id;
    }
    else{
        echo "Login not successful";
        return false;
    }
}

// user id from username
function id_from_username($username){
    $username = sanitize($username);
    $query = "SELECT `user_id` FROM `user_data` WHERE      `username` = '$username'";
    $result = mysqli_query($conn,$query);
    $row = mysqli_fetch_array($result, MYSQLI_NUM);
    return $row[0];
}
  • 写回答

1条回答 默认 最新

  • dongrong7267 2018-08-25 09:15
    关注

    EDIT: As Lawrence correctly pointed out I missed the variable scope issue. mysqli_query does not have access to $conn variable.


    Try to check the number of returned rows in the $result:

    echo $result->num_rows;
    

    Or print it to the log if you have some. Not sure how you are checking mysqli_fetch_array does not return values - try var_dump() or print_r().

    Few further recommendations:

    • Both queries in your question select from the same table - user_data - it is pretty inefficient to do it separately (unless id_from_username is commonly used elsewhere), I'd merge that into one query to select it at once:

      SELECT user_id, password FROM user_data WHERE ...

    • Using mysqli_query and concatenating user input into the query is usually a bad idea. Not sure what your sanitize function does but I'd still use variable binding instead, even mysqli supports that with mysqli_prepare, see here.

    • Based on your code you are storing passwords in database using md5. This is a very bad practice. PHP provides very nice functions password_hash and password_verify since PHP 5.5.0, they will handle password hashing for you and make your application a lot more secure.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?