Reputation: 923
I am trying to get the data from database in one single array but I was unsuccessful doing that. What I have tried is -
$q = mysql_query("SELECT*FROM meaning ORDER BY RAND() LIMIT 7");
$gt = mysql_fetch_array($q);
var_dump($gt);
This query fetches only one row. What I want is that this query should fetch random 7 rows and come back in one array as data like -
array(4) { [0]=> row_0
[1]=> row_1
[2]=> row_2
[3]=> row_3
[4]=> row_4
[5]=> row_5
[6]=> row_6
}
Upvotes: 0
Views: 6939
Reputation: 789
mysql_fetch_array returns the first row in a MySQL Resource in the form of an associative array. Fetches a result row as an associative array, a numeric array, or both.
You need to use loop to get all the records.
$q = mysql_query("SELECT * FROM meaning ORDER BY RAND() LIMIT 7");
while ($row = mysql_fetch_array($q)) {
echo $row["title"]."<br />";
}
Upvotes: 0
Reputation: 11675
Try this:
$q = mysql_query("SELECT * FROM meaning ORDER BY RAND() LIMIT 7");
$i = 0;
$myarray[]='';
while($gt = mysql_fetch_assoc($q))
{
$myarray[$i] = $gt;
$i++;
}
var_dump($myarray);
Upvotes: 0
Reputation: 22741
Hope this may helps you,
$q = mysql_query("SELECT * FROM meaning ORDER BY RAND() LIMIT 7");
while($gt = mysql_fetch_assoc($q)){
$myarray[] = $gt;
}
var_dump($myarray);
Upvotes: 0
Reputation: 782673
There is no function in the mysql
extension that does what you want. All the mysql_fetch_XXX
functions read just one row at a time. To get everything, you have to use a loop:
$gt = array();
while ($row = mysql_fetch_assoc($q)) {
$gt[] = $row;
}
var_dump($gt);
If you convert to the PDO extension, it has the method PDO::fetchAll
that does what you want.
Upvotes: 8