0

I'm about ready to pull my hair out, I can not figure out why this is giving me an error, I do all my other SQL queries like this without a single problem. What am I missing here?

<?php
require_once('config.php');
$con = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
mysql_select_db("tpeoria_main", $con);
$result = mysql_query("SELECT * FROM message WHERE to=1");
while($row = mysql_fetch_array($result)){
echo "<tr><td>".$row['from']."</td><td>".$row['body']."</td></tr>";
}
mysql_close($con);
?>

Structure

Database

Tyler Siegrist
  • 139
  • 4
  • 10

3 Answers3

4

to is a reserved word.

You need to encase it in tick marks.

WHERE `to` = ...

You also need to stop using mysql_ functions. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which.

Kermit
  • 33,827
  • 13
  • 85
  • 121
1

The previous is correct. ADO is definitely preferred, but in answer to your question, you need to use "_fetch_row()" rather than "_fetch_array()." When you call "_fetch_array()" there is only one result and nothing to iterate through in your loop.

1

the problem is here "$result = mysql_query("SELECT * FROM message WHERE to=1");".. you can not use a reserved word just like this..use quotes for this type of reserved words.. like 'to'. refer the link http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html for other reserved words in mysql

DjangoDev
  • 889
  • 6
  • 16
  • 25