PHP / mySQL Like函数不起作用

时间:2011-12-07 21:10:14

标签: php mysql

我在PHP中有一个简单的查询,但我无法让Like工作。

以下是代码:

$var = $_GET['q'];
$trimmed = trim($var);
$query = "SELECT * FROM vm_regiony WHERE nazev LIKE "%$trimmed%" order by id     LIMIT 10";
$result = mysql_query($query);
if(mysql_num_rows($result)==0){
  echo "nothing";
  echo "<br />";
  echo $trimmed;
}else{
  while($rene=mysql_fetch_array($result)){
    $jmeno = $rene['nazev'];
    echo '<a id="hled" onclick="javascript:vybrat()">'.$jmeno.'</a>';

2 个答案:

答案 0 :(得分:5)

如果您需要使用单引号

$query = "SELECT * FROM vm_regiony WHERE nazev LIKE '%$trimmed%' order by id LIMIT 10";

答案 1 :(得分:0)

$query = "SELECT * FROM vm_regiony 
          WHERE nazev LIKE '%' . $trimmed . '%' 
          ORDER BY id LIMIT 10";