这个动态查询代码,问题在哪里(php)?

时间:2011-02-08 03:11:54

标签: php mysql dynamic-sql

我在搜索框上有动态查询的代码,但我没有搜索结果的任何结果(这个单词必须返回几条记录),而我有输出字符串($ q),感谢帮助我找到问题。

<?php

error_reporting(E_ALL);

//include("../mahal/mahal.php"); 
include("../mahal/mahalmag.php");

$textfieldstring = mysql_real_escape_string($_REQUEST['Textfield1']);

$connmag = mysql_connect($dbhostmag, $dbusermag, $dbpassmag) or die ('Error connecting to Database,this is maybe due to heavy traffic,please refresh your page.'); 
$dbname = 'magazine';
mysql_select_db("$dbname");
mysql_query('SET NAMES utf8',$connmag);  //for utf charachter 

$q = strip_tags(trim($textfieldstring));

if($q) {
      $search_words = explode(' ', strtolower($q));

      for($i = 0; $i < count($search_words); $i++) {
            if($i) {
                  $str_search .= "OR title LIKE '%".mysql_real_escape_string($search_words[$i])."%' OR articlebody LIKE '%".mysql_real_escape_string($search_words[$i])."%' ";
            } else {
                  $str_search = "OR title LIKE '%".mysql_real_escape_string($search_words[$i])."%' OR articlebody LIKE '%".mysql_real_escape_string($search_words[$i])."%' ";
            }
      }
}
$search_data = "WHERE (".$str_search.")";

$query_search = "SELECT a.articleid, a.title, a.state, SUBSTR(a.articlebody,1,300) AS description1, 
      p.photofilename, a.deletitem, ac.catid,c.parentid,a.articlebody
    FROM tbarticles a
      INNER JOIN tbphotos p ON (p.articleid = a.articleid)
      INNER JOIN tbarticlecategories ac ON (ac.articleid =  a.articleid)
      INNER JOIN tbcategories c ON (c.catid = ac.catid)
    $search_data AND a.deletitem = '1'";

$result_search = mysql_query($query_search);
$num_rows = mysql_num_rows($result_search);
$query_row = mysql_fetch_assoc($result_search);
echo "$num_rows";
echo "$q";
?>

更新:从$i为0的情况中删除了额外的“OR”。

当搜索词是(例如)“cold”时,生成的SQL查询是:

SELECT a.articleid, a.title, a.state, SUBSTR(a.articlebody,1,100) AS description1,
       p.photofilename, a.deletitem, ac.catid,c.parentid,a.articlebody 
  FROM tbarticles a 
    INNER JOIN tbphotos p ON (p.articleid = a.articleid) 
    INNER JOIN tbarticlecategories ac ON (ac.articleid = a.articleid) 
    INNER JOIN tbcategories c ON (c.catid = ac.catid) 
  WHERE ( title LIKE '%cold%' OR articlebody LIKE '%cold%' ) 
    AND a.deletitem = '1'

搜索字词为“冷播放”,查询为:

SELECT a.articleid, a.title, a.state, SUBSTR(a.articlebody,1,100) AS description1, 
       p.photofilename, a.deletitem, ac.catid,c.parentid,a.articlebody 
  FROM tbarticles a 
    INNER JOIN tbphotos p ON (p.articleid = a.articleid) 
    INNER JOIN tbarticlecategories ac ON (ac.articleid = a.articleid) 
    INNER JOIN tbcategories c ON (c.catid = ac.catid) 
  WHERE ( title LIKE '%cold%' OR articlebody LIKE '%cold%' 
       OR title LIKE '%play%' OR articlebody LIKE '%play%' ) 
    AND a.deletitem = '1' but lot of records are not related to search word

1 个答案:

答案 0 :(得分:0)

您忘记从$ str_search

中删除第一个OR
  $search_data = "WHERE (".$str_search.")";

代码应该是

  $str_search = '';
  for($i = 0; $i < count($search_words); $i++) {
        if($i) { $str_search .= " OR "; }
        $str_search .= " title LIKE '%".mysql_real_escape_string($search_words[$i])."%' OR articlebody LIKE '%".mysql_real_escape_string($search_words[$i])."%' ";
  }

要使其匹配多个$ search_words,其中所有单词必须存在于单个文章正文或单个标题中,那么

  $str_search_article = '';
  $str_search_title = '';
  for($i = 0; $i < count($search_words); $i++) {
        if($i) {
            $str_search_article .= " AND ";
            $str_search_title .= " AND ";
        }
        $search_word = "'%".mysql_real_escape_string($search_words[$i])."%'";
        $str_search_title .= " title LIKE " . $search_word;
        $str_search_article .= " articlebody LIKE " . $search_word;
  }
  $search_data = "WHERE ((".$str_search_title.") OR (".$str_search_article."))";