Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

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

Engaged ,
Feb 12, 2014 Feb 12, 2014

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

TOPICS
Server side applications
2.2K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Mentor , Feb 12, 2014 Feb 12, 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.

Translate
LEGEND ,
Feb 12, 2014 Feb 12, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Engaged ,
Feb 13, 2014 Feb 13, 2014
LATEST

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Mentor ,
Feb 12, 2014 Feb 12, 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines