简体   繁体   中英

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?

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

Reference: PHP String Parsing

use like this

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

you can find difference using echo $query1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM