PHP从数据库搜索

时间:2016-01-16 16:21:49

标签: php html mysql

  <!DOCTYPE html>
  <html lang="en">

   <head>
   <meta charset="utf-8" />
   <title>TEST QUERY</title>
   </head>

   <body>
     <form action="" method="GET">
Search:
<input type="text" name="query" />
<br /> Please Enter Last Name
<input type="submit" value="Submit" />
 </form>

  <?php
  $db_hostname = 'localhost';
  $db_username = 'zvrub';
  $db_password = 'zvrub';
  $db_database = 'dbtest';

  $con = mysql_connect($db_hostname,$db_username,$db_password,$db_database);
  if (!$con)
  {
  die('Could not connect: ' . mysql_error());
   }

 mysql_select_db($db_database, $con);
 $query = $_GET['query']; 
 $min_length = 1;
 if(strlen($query) >= $min_length){ 
     $query = htmlspecialchars($query); 
     $query = mysql_real_escape_string($query);
     $raw_results = mysql_query("SELECT * FROM 'HR DATA'
     WHERE (`LAST_NAME` LIKE '%".$query."%') OR (`LAST_NAME` LIKE  
     '%".$query."%')") or die(mysql_error());                                      
      if(mysql_num_rows($raw_results) > 0){ 
      while($results = mysql_fetch_array($raw_results)){
      echo "<p><h3>".$results['title']."</h3>".$results['text']."</p>";

    }

    }
    else{ 
    }

}
else{ 
    echo "Minimum length is ".$min_length;
}
 ?>
 </body>

 </html>

我有这段代码,但它给出了一个错误:你的SQL语法有错误;检查与您的MySQL服务器版本相对应的手册,以便在HR&#39; HR DATA&#39;附近使用正确的语法。哪里(LAST_NAME喜欢&#39;%布莱恩特%&#39;)或(LAST_NAME喜欢&#39;&#39;在第1行

1 个答案:

答案 0 :(得分:1)

尝试使用反引号(`)而不是单引号(')来分隔表名

"SELECT * FROM `HR DATA`