0

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?

1
  • Is there a reason you aren't using parameterized queries? Commented Mar 26, 2013 at 4:14

2 Answers 2

3

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

Reference: PHP String Parsing

Sign up to request clarification or add additional context in comments.

1 Comment

Well done. That did it. I'll select it as the answer as soon as the site lets me.
0

use like this

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

you can find difference using echo $query1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.