Jump to content


Photo

Displaying random records from a table


  • Please log in to reply
3 replies to this topic

#1 go2go

go2go
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 27 October 2006 - 04:31 AM

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

#2 redarrow

redarrow
  • Members
  • PipPipPip
  • Advanced Member
  • 7,308 posts
  • Locationlondon

Posted 27 October 2006 - 06:27 AM

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.


$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");


And this one for all the tables round.


$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");


Wish i new all about php DAM i will have to learn
((EMAIL CODE THAT WORKS))
http://simpleforum.ath.cx/mail2.inc
((PAYPAL INTEGRATION THAT WORKS))
http://simpleforum.a...aypal1_info.inc

#3 go2go

go2go
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 27 October 2006 - 03:42 PM

Thanks for your guide. I created a file called test.php as below:

<?
// 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");
?>

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.

#4 akitchin

akitchin
  • Staff Alumni
  • Advanced Member
  • 2,516 posts
  • LocationCalgary, AB, Canada

Posted 27 October 2006 - 03:47 PM

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.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users