I am trying to SELECT all the rows in my MySQL where a field matches a variable using the db_query() function. The variable is not a user input, it is retrieved from another database table based on the user currently logged on, so basically the variable is safe, but it does contain hyphens. Obviously without the hyphens the queries won't work properly, but for some reason if I include the variable the database query stops working.
When I say it stops working, I can use the functions in the object that is returned but whenever I try to get individual fields I get an error. In case it affects any suggestions, the query has two more 'comparisons' but without the third, containing hyphens, the query could return more than one row. I only want either one or zero rows to be returned.
Anybody any ideas?
UPDATE: The code I have looks something like this:
global $user;
$ukey = "A-B-C";
$result = db_query("SELECT NAME FROM ukeys WHERE uid = :uid AND EMAIL = :email AND GENERATEDKEY = :key", array(
':uid' => $user->uid,
':email' => $user->mail,
':key' => $ukey
));
if($result->rowCount() == 1){
$name = $result->NAME;
And the error comes on the bottom line where I try to get the NAME from the results.
$query=db_query('SELECT * from {foo} WHERE bar = %s', $bar);
and $bar is the variable in question, it will most probably fail if it has hyphens in it because you aren't quoting it in your sql, eg, you should be doing$query=db_query("SELECT * from {foo} WHERE bar = '%s'", $bar);
(or whatever the d7 equivalent is, but the point being make sure you are quoting the variable you are comparing against.)