Jump to content

Displaying random records from a table


go2go

Recommended Posts

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]
Link to comment
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.
Link to comment
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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.