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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

mysql, pull/display random entry

Reply to Message Icon

Name: fleetmack
Date: January 3, 2007 at 21:35:23 Pacific
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>



Sponsored Link
Ads by Google

Response Number 1
Name: Laler
Date: January 4, 2007 at 08:01:30 Pacific
Reply:

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


0

Response Number 2
Name: fleetmack
Date: January 4, 2007 at 09:01:21 Pacific
Reply:

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



0

Response Number 3
Name: Laler
Date: January 4, 2007 at 09:34:16 Pacific
Reply:

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


0

Response Number 4
Name: fleetmack
Date: January 4, 2007 at 10:14:22 Pacific
Reply:

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


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


Control Panel What operating system sup...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: mysql, pull/display random entry

php section disappears www.computing.net/answers/webdevel/php-section-disappears/2953.html

random ads www.computing.net/answers/webdevel/random-ads/3983.html

Is this possible with PHP www.computing.net/answers/webdevel/is-this-possible-with-php/1951.html