Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4122916
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:32:38+00:00 2026-05-20T23:32:38+00:00

I have got data from mysql_fetch_assoc stored in to an array using this command

  • 0

I have got data from mysql_fetch_assoc stored in to an array using this command

if(mysql_num_rows($data) > 1){
                while($row = mysql_fetch_assoc($data)){
                    $ndata[] = $row;
                }
            } else {
                $ndata = mysql_fetch_assoc($data);
            }

Now when I use count on $ndata, it retuns 1; although it is empty.

When I run mysql_num_rows on the returned data it retuns 0 rows. But when I convert the data to $ndata and then run count on that it returns 1. I want it to return the number of rows.

Thanks

Can someone please explain why is there a problem and how to fix it?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:32:38+00:00Added an answer on May 20, 2026 at 11:32 pm

    Your logic is wrong: you don’t test for the case that mysql_num_rows($data) == 0. If this is the case, your code executes the same path as when the number of rows is 1 ($ndata = mysql_fetch_assoc($data);). But there are no more rows to return (there are no rows at all), so mysql_fetch_assoc returns false. And count(false) returns 1, because that’s how count works.

    Do it this way:

    $rows = mysql_num_rows($data);
    if($rows == 0) {
        return null;
    }
    else if($rows == 1){
        $ndata = mysql_fetch_assoc($data);
    } else {
        while($row = mysql_fetch_assoc($data)){
            $ndata[] = $row;
        }
    }
    

    You can either return null or array() in the first if branch; these are the only two values for which count returns 0.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So i have a .wav file. from this i got the data: as expected
I've got this php page where I do a search which returns data from
I have a mySQL database from where I fetch some data via PHP. This
I have already created a report using list of data from my database. i
I have queried yahoo finance for historic data and have got JSon output. What
i have got <div><a class='link' href='index.php' data-container='target'>Load</a></div> <div class='target'></div> i need to write jquery
I have to submit subscription data to another website. I have got documentation on
So I've got some data. There are entities. Entities have an arbitrary number of
I've got some financial data to store and manipulate. Let's say I have 2
I've got an asp.net application where each client will have their own data entry

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.