0

I am still new in the PHP and MySQL. I just started to learn MySQLi before few days and I met this problem. This page is named login.php. The problem is that the query return 0 num rows. When I run the query in phpmyadmin it show this message and 1 row.

Showing rows 0 - 0 (1 total, Query took 0.0010 sec)

Image:

screenshot

Here is my code:

<?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';
}

Could you help me to fix this issue?

gen_Eric
  • 223,194
  • 41
  • 299
  • 337

2 Answers2

0
$sql = mysqli_query($connection, $query);
$count = $sql->num_rows; // $count contains now your value
mkabanen
  • 615
  • 5
  • 15
0

You are using prepared statements, so the syntax is slightly different. You want to use mysqli_stmt_num_rows instead of 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);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
  • Thanks for the correction, but it returned 0 again. Here is var_dumb of $stmt: object(mysqli_stmt)#2 (10) { ["affected_rows"]=> int(-1) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(2) ["field_count"]=> int(5) ["errno"]=> int(0) ["error"]=> string(0) "" ["error_list"]=> array(0) { } ["sqlstate"]=> string(5) "00000" ["id"]=> int(1) } – user3179358 Jan 09 '14 at 22:09