MYSQLi num_rows总是返回0

时间:2014-01-09 21:30:49

标签: php mysql mysqli

我仍然是PHP和MySQL的新手。我刚开始学习MySQLi几天,我遇到了这个问题。 此页面名为login.php。问题是查询返回0 num行。 当我在phpmyadmin中运行查询时,它会显示此消息并显示1行。

  

显示行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';
}

你能帮我解决这个问题吗?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分: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);