Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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>

Your code is a bit unusual imo...
4 columns, id_number, quote, name, notesTry 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

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

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

![]() |
Control Panel
|
What operating system sup...
|

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