Marcel Marino
Marcel Marino

Reputation: 1002

PHP Query Not Understanding Integer

Here's my code:

    function display_name1($s){
        global $db;
        $query1 = 'SELECT Name From Drink where P_Key = $s';
        $r = $db->prepare($query1);
        $r->execute();
        $result = $r->fetchColumn();
        return $result;

        }

$s contains the result returned from the P_Key, an auto incremented column. I want to be able to give the query the P_Key and it will return whatever variable from that row I want. For some reason, this isn't working. It returns nothing. Now if I return $s, then it does display the numbers like it should, so the problem isn't with the $s variable itself. If I take the $s out of the query, and replace it with a number, then it returns the name of the drink just like it should, so the problem isn't with the database or the query. The problem seems to be that $s is being interpreted incorrectly.

I've tried converting it to an integer ahead of putting it into the query, no dice. Any ideas?

Upvotes: 0

Views: 65

Answers (2)

M.I.T.
M.I.T.

Reputation: 1042

use like this

"SELECT Name From Drink where P_Key = ".$s.""

you can find difference using echo $query1

Upvotes: 0

Mahmood Rehman
Mahmood Rehman

Reputation: 4331

Try to use double quotes.Single quote didn't interpret the variables."SELECT Name From Drink where P_Key = $s"

Reference: PHP String Parsing

Upvotes: 3

Related Questions