Why isn't the "message" field being pulled from my MYSQL database?

Go To StackoverFlow.com

0

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!

2012-04-04 03:58
by karhu
order by rand() only ok for small d - NoName 2012-04-04 04:08
@Dagon it's a very small database - karhu 2012-04-04 04:13


3

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.

2012-04-04 04:04
by Marc B
Thank you for your help and advice. I'll be sure to put a die function in everything from now on. Cheers - karhu 2012-04-04 04:18
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
Good to know. Thanks - karhu 2012-04-04 04:28
Ads