2014-01-09 197 views
0

我還是PHP和MySQL中的新手。我在幾天之前開始學習MySQLi,並遇到了這個問題。 此頁面被命名爲login.php。問題是查詢返回0個num行。 當我在phpmyadmin中運行查詢時,它顯示此消息和1行。MYSQLi num_rows總是返回0

顯示行0 - 0(1個總計,查詢花費0.0010秒)

圖片:

screenshot

這是我的代碼:

<?php 
session_start(); 
require 'config.php'; 
$username = $_POST['username']; 
$password = $_POST['password']; 
$salt = 'qwerty'; 
$pass_prepare = md5($salt).sha1($password); 

if ($_SERVER['REQUEST_METHOD'] != "POST") { 
    header('Location: index.php'); 
} 
if (isset($username) && isset($pass_prepare)) { 
    $stmt = mysqli_prepare($db_con, "SELECT user_id, username, password, rank, last_activity FROM imes_users WHERE username=? AND password=? LIMIT 1"); 
    mysqli_stmt_bind_param($stmt, "ss", $username, $pass_prepare); 
    mysqli_execute($stmt); 
    mysqli_stmt_bind_result($stmt, $user_id, $username1, $password1, $rank, $last_activity); 
    mysqli_stmt_fetch($stmt); 
    echo mysqli_num_rows($stmt); 
} else { 
    echo 'error'; 
} 

莫非你幫我解決這個問題?

回答

0
$sql = mysqli_query($connection, $query); 
$count = $sql->num_rows; // $count contains now your value 
+0

他正在使用準備好的語句。 –

0

您正在使用預處理語句,因此語法稍有不同。您想使用mysqli_stmt_num_rows而不是mysqli_num_rows

$stmt = mysqli_prepare($db_con, "SELECT user_id, username, password, rank, last_activity FROM imes_users WHERE username=? AND password=? LIMIT 1"); 
mysqli_stmt_bind_param($stmt, "ss", $username, $pass_prepare); 
mysqli_execute($stmt); 
mysqli_stmt_bind_result($stmt, $user_id, $username1, $password1, $rank, $last_activity); 
mysqli_stmt_fetch($stmt); 
echo mysqli_stmt_num_rows($stmt); 
+0

感謝您的更正,但它再次返回0。 這裏是$語句的var_dumb: 對象(mysqli_stmt)#2(10){ [ 「affected_rows」] => INT(-1) [ 「INSERT_ID」] => INT(0) [ 「NUM_ROWS」] => INT(O) [ 「param_count」] => INT(2) [ 「場計數」] => INT(5) [ 「錯誤號」] => INT(0 ) [「error」] => string(0)「」 [「error_list」] => array(0){ } [「sqlstate」 ] => string(5)「00000」 [「id」] => int(1) } – user3179358