Computing.Net > Forums > Web Development > mysql, pull/display random entry

mysql, pull/display random entry

Reply to Message Icon

Original Message
Name: fleetmack
Date: January 3, 2007 at 21:35:23 Pacific
Subject: mysql, pull/display random entry
OS: php4
CPU/Ram: 3.0/gig
Comment:

I have a MySQL database with simply one table .... 4 columns, id_number, quote, name, notes

I would like, in the header of my website, for a random quote to be pulled from the database upon each arrival to the page. For now, I just need it to pull a random "quote" field. The PHP code I am using apparently isn't working, can someone help? Here is my code:

<html>
<body>
<?php
@mysql_connect("localhost", "root", "my_password");
@mysql_select_db("quotes")
or die("Quote database is currently down");
//this line will store the sql statement in the variable -sql-
$quote_query = 'select quote from quotes order by RAND() limit 1';

//this line will store the result in -result-
$result = 'mysql_query($quote_query);

//keeps the array in tact
$row = mysql_fetch_array($result,MYSQL_ASSOC);

//displays the row
$quote = $row['quote'];

echo

$quote;</p>
?>

</body>
</html>


Report Offensive Message For Removal


Response Number 1
Name: Laler
Date: January 4, 2007 at 08:01:30 Pacific
Subject: mysql, pull/display random entry
Reply: (edit)

Your code is a bit unusual imo...


4 columns, id_number, quote, name, notes

Try this:

<?php

$link = mysql_connect("yourhost", "yourusername", "yourpassword") or die (mysql_error ());

mysql_select_db ('quotes', $link) or die (mysql_error ());

$sql = mysql_query ("SELECT `quote` FROM `quotes` ORDER BY RAND() LIMIT 0,1") or die (mysql_error ());

$res = mysql_fetch_assoc ($sql);

$randomquote = $res['quote'];

echo $randomquote;

?>


See if any error message comes up.

---
Fubar


Report Offensive Follow Up For Removal

Response Number 2
Name: fleetmack
Date: January 4, 2007 at 09:01:21 Pacific
Subject: mysql, pull/display random entry
Reply: (edit)

Hey it works! Thanks Laler!!

Any idea how to get it to pull a second field from the same column?

I have another field called "name" and I'd like it to pull that as well ... I tried this:

$sql = mysql_query ("SELECT `quote`, ' --', 'name' FROM `quotes` ORDER BY RAND() LIMIT 0,1") or die (mysql_error ());

but it did not work



Report Offensive Follow Up For Removal

Response Number 3
Name: Laler
Date: January 4, 2007 at 09:34:16 Pacific
Subject: mysql, pull/display random entry
Reply: (edit)

The did not work code should be like this:


$sql = mysql_query ("SELECT * FROM `quotes` ORDER BY RAND() LIMIT 0,1") or die (mysql_error ());

Then you can:

/* Fetch the result row while keeping the column names */
$res = mysql_fetch_assoc ($sql);

echo '"' . $res['quote'] . '" by ' . $res['name'];


---
Fubar


Report Offensive Follow Up For Removal

Response Number 4
Name: fleetmack
Date: January 4, 2007 at 10:14:22 Pacific
Subject: mysql, pull/display random entry
Reply: (edit)

Wonderful, absolutely wonderful. Thanks so much for your help, friend!


Report Offensive Follow Up For Removal







Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: mysql, pull/display random entry

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




How often do you use Computing.Net?

Every Day
Once a Week
Once a Month
This Is My First Time!


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge