doulu7921 2014-01-17 05:12
浏览 29

多个搜索查询php

i have a database where i have title

Titles
Celeste from the moon
Sweet Dreams Tomorrow
Time machine from past

Now when i do my search query for keyword lets say 'celeste from' then it displays me what i want i.e. Celeste from the moon. However if i change my query to 'celeste the moon' , then it shows me no results.

This is my query script

      if(!empty($_REQUEST['string'])) 
{
      $search_string = " title LIKE %".mysql_real_escape_string($_REQUEST["string"])."%'";
      $query = mysql_query("SELECT * FROM products WHERE ".$search_string);

      $row = mysql_num_rows($query) or die(mysql_error());

      $row = $row; 

      $cur_page = $_GET['page'];
      $cur_page = ($cur_page < 1)? 1 : $cur_page; 
      $offset = ($cur_page-1)*$per_page;                

      $pages = ceil($row/$per_page);              

      $start = (($cur_page - $num_links) > 0) ? ($cur_page - ($num_links - 1)) : 1;
      $end   = (($cur_page + $num_links) < $pages) ? ($cur_page + $num_links) : $pages;

      $res  =  mysql_query("SELECT * FROM products WHERE ".$search_string." ORDER BY title LIMIT ".$per_page." OFFSET ".$offset);  

      while($row=mysql_fetch_array($res))
      {
      include ('include/form.php');
      }

  }

I tried various ways but just not able to get the desired results.

  • 写回答

2条回答 默认 最新

  • dongtaijue1578 2014-01-17 05:48
    关注

    First you split the search string

    $words = explode(' ',$_REQUEST['string']);
    

    then use the OR operation in select

    $query='SELECT * FROM `products` WHERE `title` LIKE ';
    foreach($words as $key=> value){
        $query.='"%'.$value.'%" OR';
    }
    $query=rtrim($string, "OR");
    $query.='ORDER BY title LIMIT '.$per_page.' OFFSET '.$offset;
    include 'include/common.php';
    while($row=mysql_fetch_array(mysql_query($query))) {
                include ('include/form.php');
        }
    

    Nex time you use mysqli instead of mysql functions

    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算