Reputation: 5
Here is my code:
$counter = 0;
$result = mysql_query("select * from funds_backup");
echo mysql_num_rows($result)."</br>"; // <--this prints 48,173
while ($row = mysql_fetch_object($result)) {
$counter++;
$name = $row->Name;
$ticker = $row->Ticker ;
$current[$ticker] = $name;
echo $counter." ".$current[$ticker]."</br>"; //<--this prints to 48,173
}
echo count($current); // <--this prints 45,650
I cannot get mysql_fetch_object
to initialize all 48,173 rows to the php array.
I have done this with larger queries before. I have no idea why this does not work. It does not truncate the end of the array ie omit 45,651-48,173. It is random. I have used TRIM() and this does not work. It is consistent in a way too. The same rows are always omitted.
Ideas?
Upvotes: 0
Views: 117
Reputation: 3026
The mysql_*
functions are actually deprecated in PHP5.5, so you might want to consider changing your code to use PDO or MySQLi, instead.
Make sure that the field 'Ticker' is actually unique, otherwise values will keep getting overwritten. Also, are you getting any memory exhausted errors while running this? That might be one cause of this happening.
What does echo $counter;
return? If it is 48,173 then the values are getting overwritten.
Upvotes: 0
Reputation: 9200
Sounds like you have multiple rows that contain the same data - which means that the value of $ticker
ends up the same more than once. In this case you are then overwriting the previous array entry with that key. Try $current[] = $name;
or $current[$counter] = $name;
to ensure that all array keys are unique.
Upvotes: 1