使用mysqli_result()获取特定列数据

时间:2014-05-28 18:53:27

标签: php mysql sql

我有一个验证用户登录的功能。

$login_query = mysqli_query($GLOBALS['conn'],
"SELECT COUNT(`user_id`) as `count`,`user_id` FROM `users`
     WHERE `user_email`='$email' AND `user_password` = '" . md5($password) . "'") or die(mysqli_error($GLOBALS['conn']));
return ($mysqli_result->num_rows == 1) ? 
           mysqli_result($login_query, 0, 'user_id') : false;

我正在使用mysql_*,现在切换到mysqli_*

我想知道的是,在return语句中如何从已从DB中选择的行返回user_id

1 个答案:

答案 0 :(得分:0)

我已经简化了一下查询。如果登录成功或者" 0"此功能将返回user_id。如果不。

在配置文件中创建$ mysqli连接,您可以将其包含在每个页面的顶部

#config.php#

$dbhost =   "localhost";
$dbuser =   "";
$dbpassword =   "";
$dbdatabase =   "";
$salt       =       "";
$mysqli = mysqli_connect($dbhost,$dbuser,$dbpassword,$dbdatabase);
if (mysqli_connect_errno()) 
{
    echo '<p>There was a problem with the mysqli connection: 
           <br>&emsp;'.mysqli_connect_error().'</p>';
    exit;
}   

   #Calling the function:#
   require_once($_SERVER['DOCUMENT_ROOT'].'/config.php');
   checklogin($email, $password, $salt, $mysqli);


   function checklogin($email, $password, $salt="", $mysqli)
   {

    $email = $mysqli->real_escape_string($email);
    $password = $mysqli->real_escape_string($password);

    $login_query = '
                SELECT * 
                FROM 
                       `users`
                WHERE 
                       `user_email`='$email' 
                AND 
                       `user_password` = "' . md5($salt.$password) . '";';

    $result = $mysqli->query($login_query)
    $row = $result->fetch_array();
    if ($result->num_rows > 0)
    {$user_id = $row['user_id'];}
    else
    {$user_id = 0;}

    return $user_ID

    }