0

I have a mysql syntax error that tells me this: "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 '= 'upVote'' at line 1"

Here is my code:

    $likedQuery = mysql_query("SELECT Liked FROM " . $cookie . "WHERE type = 'upVote'", $dbh2) or die (mysql_error());
$likedArray = mysql_fetch_array($likedQuery);

$allLikes = $likedArray['Liked'];
  • 3
    Type is a keyword. Escape it. – karthikr Jul 29 '13 at 04:22
  • 3
    This question appears to be off-topic because it is about syntax errors – karthikr Jul 29 '13 at 04:23
  • @karthikr Was just checking ... it's not on [the list](http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html) but a good idea nontheless – Orangepill Jul 29 '13 at 04:23
  • what is the value of $cookie – Orangepill Jul 29 '13 at 04:23
  • The value of $cookie is a username. Each user has a database table. – Arnar Kjartansson Jul 29 '13 at 04:26
  • 2
    Ohh yeah and obligitory [mysql_* is bad](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php/14110189#14110189) comment. All the cool kids are switching to [mysqli](http://php.net/manual/en/book.mysqli.php) or [pdo](http://php.net/manual/en/book.pdo.php) – Orangepill Jul 29 '13 at 04:29
  • @karthikr `type` is not a [reserved word](http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html). – Ja͢ck Jul 29 '13 at 04:35
  • @ArnarKjartansson That comment turns this from "suspicious looking code" into "a database design that's clearly wrong" – Jasper Jul 29 '13 at 05:02

3 Answers3

1

You should add a space before the WHERE, change:

$likedQuery = mysql_query("SELECT Liked FROM " . $cookie . "WHERE type = 'upVote'", $dbh2) or die (mysql_error());

to:

$likedQuery = mysql_query("SELECT Liked FROM " . $cookie . " WHERE type = 'upVote'", $dbh2) or die (mysql_error());
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
1

You are missing a space after your table name

$likedQuery = mysql_query("SELECT `Liked` FROM `" . $cookie . "` WHERE `type` = 'upVote'", $dbh2) or die (mysql_error());
$likedArray = mysql_fetch_array($likedQuery);

$allLikes = $likedArray['Liked'];

And it's always a good idea to escape your field and table identifiers with a backtick

Orangepill
  • 24,500
  • 3
  • 42
  • 63
0

you probably need a space between $cookie and your WHERE

$cookie . " " . WHERE
Jacob
  • 920
  • 6
  • 19