Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 06-16-2012, 08:08 PM   PM User | #1
kenno69
New to the CF scene

 
Join Date: Feb 2009
Posts: 8
Thanks: 4
Thanked 0 Times in 0 Posts
kenno69 is an unknown quantity at this point
Mysqli - using select case but other data is not bound

I'm using Mysqli OO to search the database for 2 specific values using the MySQL statement CASE. If both values are found then it will return the value 'true', if only one is found the value is 'false', if neither are found then the result is neither. Anyway I've got this working but my problem is I need it to also display the values that are related in the column.

PHP Code:
$query "SELECT 'userName','email','code',
CASE 
    WHEN (email=? AND code=?) THEN 'true'
    WHEN (email=?) THEN 'false'
END AS answer
FROM users WHERE email=?"
;
$stmt $mysqli->prepare($query);
$stmt->bind_param("ssss"$email,$code$email,$email);
$email $mysqli->real_escape_string($email);
$code $mysqli->real_escape_string($code);
$stmt->execute();
$stmt->bind_result($user,$email,$code,$answer);
$stmt->fetch();
print_r($stmt->fetch);
$stmt->store_result();
echo 
"User: ".$user." Email: ".$email." Code: ".$code." Answer: ".$answer
Okay so when I run the code it should display as:

Code:
User: ryan Email: local@localhost Code: 12 Answer: true
But instead it displays it as:

Code:
User: userName Email: email Code: code Answer: true
So I think the error is more to do with the SQL side of it but not sure, I'm stuck and would like some help. Thanks

BTW I have posted it in the PHP area as well just for the simple reason I'm not entirely sure which part it is, so sorry if I'm not allowed to do this

Last edited by kenno69; 06-16-2012 at 08:10 PM..
kenno69 is offline   Reply With Quote
Old 06-16-2012, 09:08 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,552
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
Has nothing to do with bind or mysqli.

When you use apostrophes around anything in SQL (any SQL, not just MySQL), you are creating a STRING. A literal string. Exactly what is in the apostrophes.

In other words, if you use
Code:
    SELECT 'username';
You *WILL* get a result of exactly username.

You surely need to and meant to use backticks (the ` character that shares a keyboard key with the ~ tilde).

Code:
$query = "SELECT `userName`,`email`,`code`, ...
But, really, there is no reason to even use them here. You would get the same (correct) results using
Code:
$query = "SELECT userName,email,code, ...
You only need to use the backticks when you use a MySQL keyword as a field name or when your field name contains characters (such as spaces, minus signs, periods) that aren't normally valid in field (and table) names.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Users who have thanked Old Pedant for this post:
kenno69 (06-17-2012)
Old 06-17-2012, 12:59 AM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,552
Thanks: 62
Thanked 4,054 Times in 4,023 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
You have another problem in the SQL, by the by.

Code:
CASE 
    WHEN (email=? AND code=?) THEN 'true'
    WHEN (email=?) THEN 'false'
END AS answer
What happens if email is *NOT* equal to the given value?

You have no WHEN to handle that, so answer will be NULL.

Not sure your CASE really makes sense, as given.
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is online now   Reply With Quote
Reply

Bookmarks

Tags
mysqli, php, sql

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 12:33 AM.


Advertisement
Log in to turn off these ads.