Jump to content

Recommended Posts

I have a query that should have 10 results.  I make the query, then count the results with mysql_num_rows().  It says 10, that's good.  Then as soon as I do mysql_fetch_array, then count that, it's only 2.  Makes no sense, here's there relevant code:

 

$table_items=mysql_query("SELECT itemnum FROM stauctioneer_tableitems WHERE `table`='$tid' ORDER BY slot ASC");
echo mysql_num_rows($table_items);  //outputs 10

$items=mysql_fetch_array($table_items);
echo count($items);  //outputs 2

I'm stumped on this one.  I've echoed the exact query and ran it through phpmyadmin, it works fine.  I've tried different values for $tid, same results.  Any help is appreciated.

Link to comment
https://forums.phpfreaks.com/topic/89328-query-problem/
Share on other sites

I just did a little test on my site.

 

<?php

$table_items = mysql_query( "SELECT `mem_id` FROM `members`" );

echo mysql_num_rows( $table_items ); // Outputs 9

 

$items = mysql_fetch_array($table_items);

var_dump( $items ); // Outputs array(2) { [0]=> string(1) "1" ["mem_id"]=> string(1) "1" }

 

echo count( $items ); // Outputs 2

 

$i = 1;

while( $test = mysql_fetch_array( $table_items ) )

{

$i++;

}

echo $i; // Outputs 9

?>

 

I think it's because you're counting the array $items... which has two parts within... for me anyway.

Link to comment
https://forums.phpfreaks.com/topic/89328-query-problem/#findComment-457424
Share on other sites

Hmm.  I just tried something else, makes even less sense now :(

 

<?php
$table_items=mysql_query("SELECT itemnum FROM stauctioneer_tableitems WHERE `table`='$tid' ORDER BY slot ASC");
echo mysql_num_rows($table_items);     //outputs 10
$items=mysql_fetch_array($table_items);
echo count($items);                            //outputs 2
print_r($items);                                  //outputs Array ( [0] => 77 [itemnum] => 77 ) 
?>

 

p.s. I need to sleep, I'll check replies in the morning.

 

 

Link to comment
https://forums.phpfreaks.com/topic/89328-query-problem/#findComment-457430
Share on other sites

pdkv2 has the answer - your row has 2 columns because mysql_fetch_array creates both a numeric array of the columns of the row and an associative array.  Since your query returns 1 column, when you try and do a count() on it, you get the answer of 2.

 

I personally use mysql_fetch_assoc()  which is a wrapper around mysql_fetch_array() that only returns the associative array version of the row.  Really if you look at the php.net manual page for either function it has an example of how you should loop through the result set fetching rows until the set is empty, as this is fairly typical code.

 

After your first fetch, you'd be able to do this:

 

echo "Item num: $items['itemnum']";

 

 

 

 

 

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/89328-query-problem/#findComment-457449
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.