doushou8730 2013-12-06 20:58
浏览 32
已采纳

php没有从数据库获取用户信息

The database information is correct and working, I've tested this several times. The database exists along with the table i am trying to pull data out of. I have dummy information in the database, here is my code to check if the user in the database 'network', table 'users':

<?php 
require 'core/init.php';

if (empty($_POST) === false){
    $username = $_POST['username'];
    $password = $_POST['password'];

    if (empty($username) === true || empty($password) === true) {
        $errors[] = 'You need to enter a username and password.';
    } else if (user_exists($username) === false) {
        $errors[] = 'Username does not exists. Have you registered?';
    } else if (user_active($username) === false) {
        $errors[] = 'Your account is not activated. Please check your email!';
    } else {

    }

    print_r($errors);
}
?>

Here is the code for the functions 'user_exists($username)'

<?php


function user_exists($username) {
    $username = sanitize($username);
    return (mysql_result(mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'username' = '".$username."'"), 0) === 1) ? true : false;
}

function user_active($username) {
    $username = sanitize($username);
    return (mysql_result(mysql_query("SELECT COUNT('user_id') FROM 'users' WHERE 'username' = '".$username."' AND 'active' = 1"), 0) === 1) ? true : false;
}
?>

sanitize function:

<?php
function sanitize($data) {
    return mysqli_real_escape_string($data);
}
?>

Here is my issue:

When I login with the dummy information - Username; Password (md5 hashed via phpmyadmin) if displays the error:

'Username does not exists. Have you registered?'

I have tried using a different database, a different user.. nothing works.. Help!

  • 写回答

3条回答 默认 最新

  • dongshi6529 2013-12-06 21:02
    关注

    Use back ticks for column and table names,not quotes.

    "SELECT COUNT(`user_id`) FROM `users` WHERE `username` = '".$username."'")
    
    return (mysql_result(mysql_query("SELECT COUNT('user_id') 
    FROM 'users' WHERE 'username' = '".$username."'"), 0) === 1) ? true : false;
    }
    

    mysql_results returns either a cell or false,so above the condition ===1 is never reached.

    Docs

    Returns the contents of one cell from a MySQL result set on success, or FALSE on failure.

    return (mysql_result(mysql_query("SELECT COUNT('user_id') 
    FROM 'users' WHERE 'username' = '".$username."'"), 0) == false) ? false: true;
    }
    

    Also you are connecting with mysql and using mysqli_real_escape_string in the sanitize function. Dont mix them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 前端echarts坐标轴问题
  • ¥15 CMFCPropertyPage
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题
  • ¥15 谁会P4语言啊,我想请教一下
  • ¥15 这个怎么改成直流激励源给加热电阻提供5a电流呀
  • ¥50 求解vmware的网络模式问题 别拿AI回答
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳