开发者

CakePHP 1.3 query method gives SQL 1064 error

开发者 https://www.devze.com 2023-03-10 13:28 出处:网络
I wrote a custom query to use in CakePHP 1.3 The query is as follows: SELECT artists.id, artists.name, artists.image_id, genres.genre

I wrote a custom query to use in CakePHP 1.3 The query is as follows:

SELECT artists.id, artists.name, artists.image_id, genres.genre
FROM artists
  LEFT JOIN coupling_artist_genre
    ON artists.id = coupling_artist_genre.id_in
  LEFT JOIN genres
    ON coupling_artist_genre.id_out = genres.id
  WHERE artists.name LIKE '%tee%'
    AND genres.id IN (12,14)
  ORDER BY artists.name ASC
  LIMIT 0,25

When I call this like this:

$this -> Artist -> query ($sql);

I get this error:

1064: 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 '

However, when I copy the generated query and paste it into PHPMyAdmin it works fine. No warnings, no 开发者_Go百科errors, and most importantly: the result I expect.

Does anyone know what could cause this difference between Cake and PMA?

Edit: This is how the query is generated:

$query = "SELECT artists.id, artists.name, artists.image_id";

if ($genres != ' ' && strlen ($genres) > 0)
{
  $query .= ", genres.genre";
}

$query .= " FROM artists";

if ($genres != ' ' && strlen ($genres) > 0)
{
  $query .= " LEFT JOIN coupling_artist_genre ON artists.id = coupling_artist_genre.id_in";
  $query .= " LEFT JOIN genres ON coupling_artist_genre.id_out = genres.id";
}

$query .= " WHERE";

if ($searchString != '' && strlen ($searchString) > 0)
{
  $searchString = $searchString == ' ' ? '' : $searchString;
  $query .= " artists.name LIKE '%".$searchString."%'"; 
}

if ($searchString != ' ' && strlen ($searchString) > 0 && $genres != ' ' && strlen ($genres) > 0)
{
  $query .= " AND";
}

if ($genres != ' ' && strlen ($genres) > 0)
{
  $query .= " genres.id IN (".$genres.")";
}

$query .= " ORDER BY artists.name ASC LIMIT " . ($page - 1) * 25 . ",25";

$this -> set ('artists', $this -> Artist -> query ($query));


I think there are case when your queries end up like this:

" ...
  WHERE 
  ORDER BY ... "

or

" ...
  WHERE artists.name LIKE '%tee%'
    AND
  ORDER BY ... "

Try this:

$query .= " WHERE True ";

if ($searchString != '' && strlen ($searchString) > 0)
{
  $searchString = $searchString == ' ' ? '' : $searchString;
  $query .= " AND artists.name LIKE '%".$searchString."%'"; 
}


if ($genres != ' ' && strlen ($genres) > 0)
{
  $query .= " AND genres.id IN (".$genres.")";
}

$query .= " ORDER BY artists.name ASC LIMIT " . ($page - 1) * 25 . ",25";

$this -> set ('artists', $this -> Artist -> query ($query));


Tried to post this earlier, but I couldn't without 100 reputation. Anyway here's the answer I tried to post earlier:

Dear visitors of this question. I am terribly sorry (especially for you ypercube because you took the time to think about AND answer my question!).

I just discovered that I made a very stupid mistake. Down in my controller, in a totally different method that shouldn't even be executed I had another query in a die() call. That was the query MySQL was complaining about. I never expected it to be executed but alas, it was the query causing my error. Seeing as I didn't need it anymore I deleted it and my error was gone.

Once again, I'm sorry for all of you that put time in this question. I shall try to be more careful in the future!

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号