Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and the Yahoo Answers website is now in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

?
Lv 5

PHP - count() is returning double the amount of elements?

This is my code: http://pastebin.com/j1eKCAm7

For some reason, count($row) returns 14 instead of 7, though there are only 7 columns in the table, shown here: http://puu.sh/5CLM9/440a856ada.png

Instead of this printing all the data from the specific row to the screen, it prints all the data, followed by 7 "Undefined offset" errors: http://puu.sh/5CLR9/e909d9d72d.png

Does anyone know why this is happening, and how to fix it?

Update:

I'm using PDO, not the mysql or mysqli system.

2 Answers

Relevance
  • 8 years ago
    Favorite Answer

    Basic concept.

    Please use mysql_num_rows which count number of rows from result set.

    e.g

    In your code do somthing like this after query

    $result = $query->execute(array(':id' => $id));

    $num_rows = mysql_num_rows($result);

    echo $num_rows;

    And use $num_rows in your for loop.

    Always try to debug code line by line

    e.g

    print your query to see any issue with it

    dump_var( objects );

    print_r( array );

  • Tenax
    Lv 5
    8 years ago

    If you retrieve the results of a query using the default MYSQL_BOTH parameter, your returned array will contain each column twice, once indexed by the column name, the other time by a numeric index.

    You have to check the code inside your _dbConnect.php for a call of mysql_fetch_array and adjust that, or iterate over the result differently.

    http://www.php.net/manual/de/function.mysql-fetch-...

Still have questions? Get your answers by asking now.