somebody2 — 2011-05-28T09:04:14-04:00 — #1
I just upgraded to PHP 5.2 and the following query doesn't seem to work anymore. I just get an error that there is an error in the syntax. Any clue why, or how to fix it?
$db = DB::getInstance ();
$sql = "SELECT *
WHERE name NOT Like :myname
$stmt = $db->query($sql);
$stmt->bindParam (':myname', $name, PDO::PARAM_STR );
$result = $stmt->fetchAll(PDO::FETCH_ASSOC);
When I use phpMyAdmin the query works, but it doesn't work in the code. I checked and the $name variable does have a value.
I get this error - "SQLSTATE: Syntax error or access violation: 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 ':myname' at line 50"
guido2004 — 2011-05-28T09:07:08-04:00 — #2
What is the exact error you get?
somebody2 — 2011-05-28T09:16:12-04:00 — #3
SQLSTATE: Syntax error or access violation: 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 ':myname' at line 50
Line 50 is - $stmt = $db->query($sql);
ibazz — 2011-05-28T11:15:36-04:00 — #4
I have only ever used LIKE and when doing so, you need to surround the value/variable with '%these jobbies%'
Say for example you want to find a word that is like 'sday', well to find those which end in 'sday' you would use '%sday' so you would get back Tuesday, Wednesday Thursday.
And if you want to find words beginning with 'Mon%', the % goes at the end, where #mon' would join with 'ney' or 'day' giving money and monday, for example.
from table where col_name like '%sday'
I imagine that not like would work similarly so give it a try, if you like.
ibazz — 2011-05-28T11:17:46-04:00 — #5
Oh, I should've said that instead of the :myname, use 'myname' and add the % to the end if you want to find 'mynameis%' or some other words, where there are characters after the inputted word.
I'm not sure I have explained that very well.