Array from mysql rows?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Echilon
Forum Newbie
Posts: 12
Joined: Fri Nov 25, 2005 4:11 am
Location: England
Contact:

Array from mysql rows?

Post by Echilon »

Is it possible to read the results of a query that finds multiple rows into an array. I know how to read fields into an array, but what about rows? At the moment, I have this

Code: Select all

$quotenumsql = "SELECT `quote` FROM `quotes`"; 
$quotenumqry = mysql_query($quotenumsql) or die(mysql_error()); 
$numquotes = mysql_num_rows($quotenumqry); 
$quoteary = mysql_fetch_array($quotenumqry, MYSQL_NUM);
$quotearysize = count($quoteary); 
//get quote 
$quotenumber = rand(0, $numquotes-1);
$quote = $quoteary[$quotenumber];
But when I echo $quotearysize, it returns 1, instead of the 15 rows in the database. I know the SQL is correct because I check it with phpmyadmin and 15 rows were returned. I just need to get these 15 rows into an array, with 1 row per array value.
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post by shiznatix »

Code: Select all

$quotenumsql = "SELECT `quote` FROM `quotes`";
$quotenumqry = mysql_query($quotenumsql) or die(mysql_error());

while ($quoteary = mysql_fetch_assoc($quotenumqry)
{
  $quotes[] = $quoteary['quote'];
}

echo '<pre>';
print_r($quotes);
echo '</pre>';
try that. i think you just need the while loop if i did not read this wrong.
User avatar
Echilon
Forum Newbie
Posts: 12
Joined: Fri Nov 25, 2005 4:11 am
Location: England
Contact:

Post by Echilon »

Thanks alot for that, it works fine now :mrgreen:
Post Reply