-1

I don't understand why $amountOfUsers is showing as 0?

This used to work before I moved to the bind_param function... I was only using query() instad of prepare. But this is a lot safer, I just have trouble understand why this doesn't work, and how to fix it.

$stmt = $mysqli->prepare("SELECT id, expire, status, username FROM username WHERE username= ?");
$stmt->bind_param('s', $username);
$stmt->execute();

//Counting results. 0 = Invalid, 1 = Valid
$amountOfUsers = $stmt->num_rows;

The error I am getting is: $amountOfUsers isn't counting the number of results properly.

Arian Faurtosh
  • 17,987
  • 21
  • 77
  • 115
  • Try `mysqli_num_rows` instead of `num_rows` as in `$amountOfUsers = $stmt->mysqli_num_rows;` (http://php.net/manual/en/mysqli-result.num-rows.php) – Funk Forty Niner Sep 10 '13 at 22:49
  • @Fred-ii- I tried that, I get: Undefined property: mysqli_stmt::$mysqli_num_rows in ... – Arian Faurtosh Sep 10 '13 at 22:50
  • Is it possible that the user name doesn't exist in the table? – Gordon Linoff Sep 10 '13 at 22:53
  • Why do so few people check the return value of their MySQL queries? How would you know if an error had occurred? Check the return value of your `$stmt->execute()`, and then fix whatever problem is (probably) occurring there. –  Sep 10 '13 at 22:55

3 Answers3

1
$stmt = $mysqli->prepare("SELECT id, expire, status, username FROM username WHERE username= ?");
$stmt->bind_param('s', $username);
$stmt->execute();
// Store the result (so you can get the properties, like num_rows)
$stmt->store_result();
// Get the number of rows
$amountOfRows = $stmt->num_rows;

// Bind the result to variables
$stmt->bind_result($id, $expire, $status, $db_username);
// Process the variables
while($stmt->fetch()) {
    printf("%d %s %s %s\n", $id, $expire, $status, $db_username);
}
FalconC
  • 1,358
  • 2
  • 14
  • 22
0

Sometimes things don't go according to plan. Checking result codes and errors available in your library is usually more efficient for troubleshooting than asking strangers, but hopefully this stranger can help... choose one of these patterns:

A:

$result = $stmt->execute();
if (!$result) { /* handle errors */ }

B:

$stmt->execute();
if ($stmt->errno != 0) { /* handle errors */ }

C (for development troubleshooting only, not code you would leave around):

$stmt->execute();
print_r($stmt->error_list);

More info here and associated pages: http://www.php.net/manual/en/mysqli-stmt.errno.php

dkamins
  • 21,450
  • 7
  • 55
  • 59
-2

I would never in my life understand why php users are so inclined to the number of rows returned.
Especially if used only as a flag... if any data returned!

Why not to take the very returned data and see?

$sql  ="SELECT id, expire, status, username FROM username WHERE username= ?s";
$stmt = $mysqli->prepare($sql);
$stmt->bind_param('s', $username);
$stmt->execute();
$res = $stmt->get_result();
$row = $res->fetch_assoc();
if ($row)
{
    // do whatever
}

I would never understand an inclination to long and windy codes as well.
Why not to get yourself an abstraction library and get everything in one single line?

$sql = "SELECT id, expire, status, username FROM username WHERE username= ?";
if ($row = $db->getRow($sql))
{
    // do whatever
}
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • PHP devs don't use `num_rows` as a flag to see if data is returned. In OP's case, he wants to know if there's _exactly_ one row with the username, and not zero rows (user isn't registered) or multiple rows (something wrong with the table). The latter returns data, but it shouldn't be used. – FalconC Sep 11 '13 at 17:14