Skip to main content
Inspiring
February 12, 2014
Answered

How do you check mysqli_numrows($result) for zero rows?

  • February 12, 2014
  • 2 replies
  • 2205 views

I tried a  query in PHPmy admin for a single record, and it returned

Rows 0 - 0 1 total.

Did the same for a non existent record and it came up with Zero results (empty)

So it looks like you need to do some math to work out whow many rows there are, as it seems to return 0 when there is only one record and 0 when there are none.

How do you check mysql_inumrows($result) for zero rows returned?

Its possibly tied to the fact that record counts begin at 0.

H Walker

This topic has been closed for replies.
Correct answer Rob Hecker2

I don't use mysqli, but you should be able to do the following:

if (!mysqli_numrows($result) {

// no rows were returned

}

You are correct that PHPmyAdmin begins its row assignment numbers with zero, not 1, so if there are 25 rows, they are listed as rows 0 - 24, but mysqli_numrows is reporting the sum, so it  should count from 1, not zero.

Bregent, I think he was just using PHPmyAdmin to test his query.

2 replies

Rob Hecker2
Rob Hecker2Correct answer
Brainiac
February 13, 2014

I don't use mysqli, but you should be able to do the following:

if (!mysqli_numrows($result) {

// no rows were returned

}

You are correct that PHPmyAdmin begins its row assignment numbers with zero, not 1, so if there are 25 rows, they are listed as rows 0 - 24, but mysqli_numrows is reporting the sum, so it  should count from 1, not zero.

Bregent, I think he was just using PHPmyAdmin to test his query.

Participating Frequently
February 13, 2014

I'm not sure what you're asking and not sure what PHPMyAdmin has to do with anything. You're talking about using this in a php script, right? Can you please clarify and include an example of your code that is not working as expected.

>Its possibly tied to the fact that record counts begin at 0.

No. The function returns the number of rows in the result set.

Inspiring
February 13, 2014

Sorry Bregent, for being a bit unclear. I was using PHPmyadmin to test my query as I always do, especially on a delete query.

Rob Hecker realised that and gave me the correct answer.

My code (from memory as I am on a different computer today) was like the following:

if (mysql_inumrows($result) <>0 ) {

     then //  records returned

} else {

     // no records returned

}

Thanks for the answer.

Howard