Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have an issue with my MySQL query. I get an error that I cannot fix, and still need some help. This is my code in my PHP script (show_messages.php):

<?php
include_once 'config.php';
$username = @$_GET['user'];
$password = @$_GET['password'];
$message_from = @$_GET['from'];
$message_to = @$_GET['to'];

if(($login = login($username, $password)) != 1) {
    echo $login; // return the value of the error
    exit;
} else {
    $mysql = mysql_query("SELECT * FROM messages WHERE from LIKE $message_from AND to LIKE $message_to ORDER BY time ASC LIMIT 50");
    $rows = @mysql_numrows($mysql);
    echo 'MySQL Error: ' . mysql_error().'<br/>';
    if($rows > 0) {
        $i = 0;
        while($i < $rows) {
            $result_content = mysql_result($mysql, $i, 'content');
            $result_from = mysql_result($mysql, $i, 'from');
            echo $result_from.'<br/>'.$result_content.'<br/><br/>';
            $i++;
        }
    } else {
        echo 'No messages.';
    }
}

?>

This is the error that I am getting:

MySQL Error: 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 'from LIKE 8 AND to LIKE 1 ORDER BY time ASC LIMIT 50' at line 1

This is my database structure: Image of my DB structure

I don't know what to do. I am developing an app, and I just can't move on without fixing this issue beforehand. Thanks in advance.

(If anyone knows how to make the other code better, feel free to correct me, I am not a professional, I am only 15 years old :P)

EDIT 2: If anyone can help me on another thing, I am getting a warning on this line:

$rows = mysql_numrows($mysql);

I have to change it to $rows = @mysql_numrows($mysql); so it won't show the warning, but it's still a warning, and if anyone is kind enough to help me resolve it, please do. Thanks again.

share|improve this question
    
What does the messages table look like? Are from and to numbers or strings? –  maxmackie Jun 22 '13 at 19:23
    
I rewrote the whole question, and attached a link to an image. I can provide my whole SQL dump of that table. I just created it yesterday. Thanks. –  Vlad Bondarenko Jun 23 '13 at 21:27
    
The word from (your column name) is a reserved word. You need to put backticks around it like this `from`. You'll also want to put single quotes around the variables in the SQL string. –  maxmackie Jun 23 '13 at 22:14
    
@MaxMackie, that works. Thanks. If you want, make an answer and I will select it. –  Vlad Bondarenko Jun 23 '13 at 22:51
    
Glad it worked for you, I added my comment as an answer. –  maxmackie Jun 24 '13 at 12:02

2 Answers 2

up vote 1 down vote accepted

The word from (your column name) is a reserved word. You need to put backticks around it like this from. You'll also want to put single quotes around the variables in the SQL string.

share|improve this answer
    
Thanks, sir, it worked like a charm. –  Vlad Bondarenko Jun 24 '13 at 16:36

When you use Reserved Words for column names in MySQL use backticks ie ` to escape.

SELECT * FROM messages WHERE from=`8` AND to=`1` ORDER BY time ASC LIMIT 50
share|improve this answer
    
Still getting the same error. I am going to edit my question right now and add my code + DB structure. –  Vlad Bondarenko Jun 23 '13 at 21:13
    
Just edited the question. Looking forward to an answer. :D –  Vlad Bondarenko Jun 23 '13 at 21:29
    
@Vlad1k you should ask a new question instead of editing existing question –  david strachan Jun 24 '13 at 9:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.