Jump to content

Archived

This topic is now archived and is closed to further replies.

go2go

Displaying random records from a table

Recommended Posts

Can someone please help me creating a php file for the following codes:

$sql = 'SELECT * FROM library ORDER BY RAND() limit 5;';

where Database is: localhost
Database name is: lib
user: guest2
pass: guest2

Thanks

Share this post


Link to post
Share on other sites
I might be wrong here dont now try this ok.

Where it says the table name your have to specify the table to be round.


fully tested working ok.

good luck redarrow.

[code]

$db=mysql_connect('localhost','guest2','guest2');
mysql_select_db('lib',$db);

$sql = " SELECT table_name FROM library order by round() limit 5 ";

$result=mysql_query($sql)or die("query round problam");

[/code]

And this one for all the tables round.

[code]

$db=mysql_connect('localhost','guest2','guest2');
mysql_select_db('lib',$db);

$sql = " SELECT * FROM library order by round() limit 5 ";

$result=mysql_query($sql)or die("query round problam");

[/code]

Share this post


Link to post
Share on other sites
Thanks for your guide. I created a file called test.php as below:

[code]
<?
// file test.php

$db=mysql_connect('localhost','user','pass');
mysql_select_db('database',$db);

$sql = "SELECT * FROM Table_NAME order by round() limit 5";

$result=mysql_query($sql)or die("query round problam");
?>
[/code]

The code seems to be working. BUT there was no display of records!!

I need to incorporate a template to display the 5 random records.

Any idea, please.

Share this post


Link to post
Share on other sites
you need to change that query back to what you had originally.

in order to pass through the resource ID sent back from a mysql_query() function, you need to use a while() loop.  any simply PHP/MySQL tutorial out there should give you a good grounding in how this is done, and there is even one on the phpfreaks.com main website by one of our mods.  have a look.

NOTE:  i'm purposely not giving you straight code because although it would be fast and easy, if you follow a tutorial you are much more likely to be able to do this kind of thing again.

Share this post


Link to post
Share on other sites

×

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.