Jump to content

Display random entries from MySQL with PHP

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Trufa

Trufa

    Newbie

  • Members
  • Pip
  • 8 posts
Hi!

I´m quite a newbie (to the forum and programming), i would need some help if possible to the following.

I wrote this VERY simple .php to display some text.

<?php

mysql_connect("localhost", "root", "root") or die(mysql_error());

mysql_select_db("jacket") or die(mysql_error());


$result = mysql_query("SELECT * FROM quote

 WHERE id='1'") or die(mysql_error());  


$row = mysql_fetch_array( $result );

echo $row['frase'];

?>

I would like if you could give me some guidance with the following:

The WHERE id= should a ramdom id selected from the ones available in the mysql table.

Can somebody guide me in right direction?

Thanks in advice!

Juan

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
Generating Random Number - Random Number Generator - Random Number PHP
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Orjan

Orjan

    Writes binary right handed and hex left handed

  • Moderators
  • 3,299 posts
another version would be
SELECT * FROM qoute ORDER BY RAND() LIMIT 1
then the MySQL randomizes row number :-)
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall

#4
Trufa

Trufa

    Newbie

  • Members
  • Pip
  • 8 posts

WingedPanther said:


Thanks you very much but i found Orjan´s solution better (becuse its a little simpler and I´m a complete newbie). But I appreciate it!!

Juan

#5
Trufa

Trufa

    Newbie

  • Members
  • Pip
  • 8 posts

Orjan said:

another version would be
SELECT * FROM qoute ORDER BY RAND() LIMIT 1
then the MySQL randomizes row number :-)

It worked like a charm!

It was exactly what i was looking for, thank you very much!!

Regards

Juan