0

I am trying to write some PHP that will run an SQL query and then count the number of rows that query returned and store it so i can use it in an if statement. Currently i am outputting the number onto the screen for debug reasons and it always outputs as 0 whereas when i run the query directly in the database it returns over 600 rows. Why is this?

$result = $db->query('SELECT * FROM `bets` WHERE `user` = 76561198223084096');
$row_cnt = $result->mysql_num_rows;
printf("Result set has %d rows.\n",$row_cnt);

Any help is appreciated

5
  • use count(*) instead of *? Commented May 12, 2016 at 19:17
  • actually: php.net/manual/en/mysqli-result.num-rows.php Commented May 12, 2016 at 19:18
  • 1
    change $row_cnt = $result->mysql_num_rows; to $row_cnt = mysqli_num_rows($result); Commented May 12, 2016 at 19:20
  • Show the code of initialising $db. Commented May 12, 2016 at 19:23
  • I tried "change $row_cnt = $result->mysql_num_rows; to $row_cnt = mysqli_num_rows($result);" but i still get 0 rows in the output Commented May 12, 2016 at 19:34

3 Answers 3

1

... it always outputs as 0 whereas when i run the query directly in the database it returns over 600 rows.

The problem is because of this statement,

$row_cnt = $result->mysql_num_rows;

It should be,

$row_cnt = $result->num_rows;

Here's the reference:

Sign up to request clarification or add additional context in comments.

4 Comments

I still get 0 as an output from this
@Tyler Run this query SELECT * FROM `bets` WHERE `user` = 76561198223084096; in your MySQL console and see if it returns any row or not.
Just ran it and: "Showing rows 0 - 99 (646 total, Query took 0.0008 seconds.)"
@Tyler hmm, strange. Try to debug this issue using mysqli::$error. Also check whether you're properly connected to database or not i.e check the status of $db variable.
1

You can use the following solution:

$counter = mysql_query("SELECT COUNT(*) AS cnt FROM `bets` WHERE `user` = '76561198223084096'") ;
$num = mysql_fetch_array($counter);
$row_cnt= $num["cnt"];
printf("Result set has %d rows.\n",$row_cnt);

6 Comments

Are you sure you're using the same db with the code, otherwise you should have got the same result with your code too.
I use $db 100s of times in the code and it works fine for all the rest, just not at this point
Check my edit, use a single quote before the no. in the query.
Try to run my query in your db and check what it will return.
Just ran it and: "Showing rows 0 - 99 (646 total, Query took 0.0008 seconds.)"
|
0
$result = $db->query("select count(*) as c from `bets` WHERE `user` = '76561198223084096'");
$count = $result->fetch_object()->c;

echo "Result set has {$count} rows ";

3 Comments

can you check it once
I just tried the edited version and still got the error
$db instead of $mysqli

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.