bbala Posted October 27, 2007 Share Posted October 27, 2007 hi all, i'm new to php and web design. While fetching data from a database table, i use mysql_query function and store the output in a variable. Is the variable contain all the data or its just a reference? how can i access its contents without mysql_fetch_array function. I'm actually trying to print the records in random order but mysql_fetch_array function returns records sequentially! Link to comment Share on other sites More sharing options...
Synook Posted October 27, 2007 Share Posted October 27, 2007 You will have to call it using mysql_fetch_array then randomise the results. $result = mysql_query("Your query");$contents = array();while ($row = mysql_fetch_array($result)) {$contents[] = $row;}$contents = shuffle($contents); There! You have a random 2-dimensional array of the data you requested. Link to comment Share on other sites More sharing options...
justsomeguy Posted October 27, 2007 Share Posted October 27, 2007 You can also randomize the results right in the query.$result = mysql_query("SELECT * FROM table ORDER BY RAND()"); Link to comment Share on other sites More sharing options...
SpOrTsDuDe.Reese Posted October 27, 2007 Share Posted October 27, 2007 Synook's code did the same as what you just posted justsomeguy. At the very end of the code, $contents = shuffle($contents); this will randomize which gets printed first. Link to comment Share on other sites More sharing options...
Synook Posted October 28, 2007 Share Posted October 28, 2007 He knows... "You can also"Wonder which one is faster? Link to comment Share on other sites More sharing options...
justsomeguy Posted October 29, 2007 Share Posted October 29, 2007 I'm not sure. On one hand you have to sort the data structure yourself, and on the other you need to generate a random number for each record. I think the memory overhead of the array might make the database more efficient, but it probably depends how many records there are and how many fields are in the array. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.