douliangli6407
2012-03-15 21:22
浏览 139
已采纳

php / mysql搜索查询语法

I have a problem with my search query, $s_query. The user can search a type (Date, Title, or Location) which corresponds to a colum in my mysql database

$search_type =mysql_real_escape_string($_POST['type']);
$search_query =mysql_real_escape_string($_POST['search_query']);

if ($search_query == "") {
  echo "<p>Please enter a search...</p>";
  exit;}

$s_query = "SELECT * FROM `posts` WHERE `$search_type` == `$search_query` ";

$s_result1=mysql_query($s_query);

if (!$s_result1) {
    die('Invalid query: ' . mysql_error());
    //header ("Location: /UC_page.html");
}

$s_row = mysql_fetch_array($s_result1);
$s_num1=mysql_numrows($s_result1);
mysql_close();

mysql_error says: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '== 1956'

i have tried every operator possible and every kind of syntax i could find, but im stuck. at one point i got the date to work, but not any of the strings. thanks in advance.

图片转代码服务由CSDN问答提供 功能建议

我的搜索查询有问题,$ s_query。 用户可以搜索类型(日期,标题, 或者位置)对应于我的mysql数据库中的列;

  $ search_type = mysql_real_escape_string($ _ POST ['type']); 
 $ search_query = mysql_real_escape_string($ _ POST [  'search_query']); 
 
if($ search_query ==“”){
 echo“&lt; p&gt;请输入搜索...&lt; / p&gt;”; 
退出;} 
 
  $ s_query =“SELECT * FROM`posst` WHERE` $ search_type` ==`$ search_query`”; 
 
 $ s_result1 = mysql_query($ s_query); 
 
if(!$ s_result1){
 die(  '无效查询:'。mysql_error()); 
 // header(“位置:/UC_page.html");
 }

$s_row = mysql_fetch_array($ s_result1); 
 $ s_num1 = mysql_numrows(  $ s_result1); 
mysql_close(); 
   
 
 

mysql_error说: 您的SQL语法有错误; 查看与您的MySQL服务器版本相对应的手册,以便在'== 1956 '附近使用正确的语法

我已尝试过所有可能的操作符和各种类型的 我能找到的语法,但我卡住了。 at有一点我得到了工作日期,但没有任何字符串。 提前知道。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • douye1940 2012-03-15 21:28
    已采纳

    Actually you have two problems. First, you're surrounding your value with `, which is invalid. Use " or ' instead.

    Also, == is not valid syntax, you need to use a single = instead. Although, looking at what you're doing, you probably want to use LIKE instead, for a case-insensitive search.

    So this query should work:

    SELECT * FROM `posts` WHERE `$search_type` = '$search_query'
    

    Or with LIKE:

    SELECT * FROM `posts` WHERE `$search_type` LIKE '$search_query'
    
    点赞 评论
  • doutiao2540 2012-03-15 21:25

    You are putting ` (on top of the tab button) around your insert value. Those only go on the field. YOu shoudl use ' (on top of the slash button) for the value.

    点赞 评论
  • doufei16736 2012-03-15 21:28

    This might help you understand what query MySQL is expecting:

    $s_query = "SELECT * FROM `posts` WHERE `" . $search_type . "` = " . $search_query;
    

    Also make sure you add the necessary quotes to $search_query. EG: If it is a string then surround it with '.

    点赞 评论

相关推荐 更多相似问题