I'm trying to return the "message" field from a random DB entry (in the messages table of my database) in the body of my homepage.
This is my index.php file. I've verified through connect.php that the link with the database is working.
<html>
<body>
<?php
include_once('connect.php');
$quote=$_GET['message'];
$sql="SELECT message FROM messages ORDER BY RAND()LIMIT 1";
$results=mysql_query($sql, $link);
while(list($message)=mysql_fetch_array($results)){
echo $message;
}
mysql_close();
?>
</body>
</html>
Can you see what I'm doing wrong? I'm sure I've made an egregious error somewhere.
Thank you for your help!
Typo:
$sql="SELECT message FROM messages ORDER BY RAND()LIMIT 1";
^--missing a space
If you'd had even bare minimum query error handling, you'd have detected this:
$results=mysql_query($sql, $link) or die(mysql_error());
never EVER assume a query has succeeded. Even if the SQL syntax was 100% valid, there's far too many OTHER reasons for things to blow up to NOT check for failure.
or die()
is handy for debugging, but for production systems you'll want something more user friendly. the mysql error messages can leak a considerable about of things that should be kept private - Marc B 2012-04-04 04:19