sam06 Posted February 29, 2008 Share Posted February 29, 2008 Hi Guys- I've been using MySQL for a while now, only getting used to it, but there is still a couple of problems, even when I seem to copy and paste from my working code... Anyway: My Code; $result = mysql_query("SELECT * FROM email WHERE 'to'='$name'") or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Id</th> <th>Time Added</th> <th>To</th> <th>From</th> <th>Comment</th> </tr>"; // keeps getting the next row until there are no more to get while($row = mysql_fetch_array( $result )) { // Print out the contents of each row into a table echo "<tr><td>"; echo $row['id']; echo "</td><td>"; echo $row['time']; echo "</td><td>"; echo $row['to']; echo "</td><td>"; echo $row['from']; echo "</td><td>"; echo $row['comment']; echo "</td></tr>"; } echo "</table>"; Unfortunetly this keeps outputing with Id Time Added To From Comment } But no data- can anyone help me? Many Thanks, Sam Quote Link to comment Share on other sites More sharing options...
peranha Posted February 29, 2008 Share Posted February 29, 2008 Where are you setting the $name variable. It is not in the code. echo "Name is " . $name; and see if it has a value. Quote Link to comment Share on other sites More sharing options...
Ads Posted February 29, 2008 Share Posted February 29, 2008 $result = mysql_query("SELECT * FROM email WHERE 'to'='$name'") or die(mysql_error()); try taking out the Quotes around the to. $result = mysql_query("SELECT * FROM email WHERE to='$name'") or die(mysql_error()); Also test to see if the $result is working. if($result) { echo " Yay It works"; } else { echo "Sad, it doesn't work "; } Quote Link to comment Share on other sites More sharing options...
sam06 Posted March 1, 2008 Author Share Posted March 1, 2008 Where are you setting the $name variable. It is not in the code. echo "Name is " . $name; and see if it has a value. Sorry for not posting it, it obtains $name from a post at the top. echo "Name is " . $name; does work, it returns Name is [name from post from] $result = mysql_query("SELECT * FROM email WHERE 'to'='$name'") or die(mysql_error()); try taking out the Quotes around the to. $result = mysql_query("SELECT * FROM email WHERE to='$name'") or die(mysql_error()); Also test to see if the $result is working. if($result) { echo " Yay It works"; } else { echo "Sad, it doesn't work "; } Yep, its returning 'Yay it works' However, removing the quotes from to returns: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'to='????'' at line 2 ????? = the value of $name Quote Link to comment Share on other sites More sharing options...
AndyB Posted March 1, 2008 Share Posted March 1, 2008 Avoid using MySQL reserved words as database field names. to is a reserved word! http://dev.mysql.com/doc/refman/5.0/en/reserved-words.html Quote Link to comment Share on other sites More sharing options...
sam06 Posted March 1, 2008 Author Share Posted March 1, 2008 Very Strange, I've changed the field to 'newto', and found out why it's not returning anything When I run the query from the code into the SQL on phpmyadmin, it returns nothing. My table looks like: id time newto from comment 1 2008-02-29 17:16:23 Helen Sam Test 2 2008-03-01 14:26:15 Sam Helen Yep But when I run the query:SELECT * FROM email WHERE 'newto'='Helen' It returns nothing. And thats the query in my php code, so thats why it's not returning anything. What am I doing wrong? EDIT:::::: I changed the code, and now it works! The new code is: $result = mysql_query("SELECT * FROM email WHERE newto='$name'") or die(mysql_error()); Thanks to those who helped. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.