简体   繁体   中英

MySQL with php variable trouble

I'm having trouble with putting a php variable into a mysql query.

For example:

mysql_query("SELECT * FROM listings WHERE title LIKE '%ipod%'");

That works, but

$key = "ipod";
mysql_query("SELECT * FROM listings WHERE title LIKE '%$key%'");

That doesn't work.

I might be doing it wrong though. If the above is the correct way to do it, then maybe another part of my script has a typo or something like that. Any help would be great.

Your not selecting anything:

"SELECT * FROM listings WHERE title LIKE '%$key%'"

notice the *

Try this:

 $key = "ipod";
 mysql_query("SELECT * FROM listings WHERE title LIKE '%".$key."%'");

Try this:

$key = "ipod";
$results = mysql_query("SELECT * FROM listings WHERE title LIKE '$key'");
$num = mysql_num_rows($results);
echo "Received " . $num . "rows of results";

While ($row = mysql_fetch_assoc($results)) {
    echo '<pre>';
    print_r($row);
    echo '</pre>';
}
$key = "ipod";
mysql_query("SELECT * FROM listings WHERE title LIKE '".$key."'");

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