0

My simple PHP code:

$query = "SELECT 'food', 'calories' FROM 'food'";

    if($query_run = mysql_query($query)){
        echo 'Query success';
    }else {
        echo mysql_error();
    }

My error message:

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 ''food'' at line 1

Can't really find out the problem, please any help?

radek19
  • 3
  • 2
  • 4
    backticks (or nothing at all). Never single quotes for that at least. Backticks, upper left corner of keyboard. Use single quotes for strings or surrounding datetimes and the like. – Drew Oct 19 '15 at 16:59
  • 1
    [`mysql_` functions are deprecated](http://us3.php.net/manual/en/function.mysql-query.php). You should be using [`mysqli_`](http://php.net/manual/en/mysqli.query.php) functions or [`PDO`](http://php.net/manual/en/class.pdo.php). I recommend PDO because of (among other things) named placeholders for prepared statements. It makes query building and security easier. – Reed Oct 19 '15 at 17:15

2 Answers2

0
$query = "SELECT food, calories FROM food";

    if($query_run = mysql_query($query)){
        echo 'Query success';
    }else {
        echo mysql_error();
    }

Use above code.

Are you sure your table name is food, and you have columns by name food and calories.

Austin
  • 1,709
  • 20
  • 40
0

You will have to delete single quotes

$query = "SELECT food, calories FROM food";

if($query_run = mysql_query($query)){
    echo 'Query success';
}else {
    echo mysql_error();
}