Php mysql ajax搜索查询

Php mysql ajax搜索查询,php,mysql,jquery,Php,Mysql,Jquery,朋友们好,我正在使用ajax和mysql进行实时搜索。问题是,当我们写一些东西时,假设字母a,它会给出真实的结果,但当我在空格中加上一个空格,然后写任何字母,假设a,那么结果就不会出现。我们怎么能忽略搜索中的空格??我的代码是 ajax search.php mysql search1.php 在查询中使用变量之前,可以修剪空白,如: $q= trim($_POST['searchword']); //remove whitespaces from front and end 您好,谢谢您的回答

朋友们好,我正在使用ajax和mysql进行实时搜索。问题是,当我们写一些东西时,假设字母a,它会给出真实的结果,但当我在空格中加上一个空格,然后写任何字母,假设a,那么结果就不会出现。我们怎么能忽略搜索中的空格??我的代码是

ajax search.php

mysql search1.php


在查询中使用变量之前,可以修剪空白,如:

$q= trim($_POST['searchword']); //remove whitespaces from front and end
您好,谢谢您的回答,但当我使用此选项并在搜索框中留出空间时,它会显示获取每个字母表的所有记录,甚至在输入任何字母表之前。我们实现实时搜索的目的是,如果搜索框的长度为1或2个字符,则不运行搜索。大多数时候,这些搜索都没有用。
         <?php include($_SERVER["DOCUMENT_ROOT"]."/sexy/include/connection.php");
     if($_POST)
     {
      $q=$_POST['searchword'];
     $sql_res=mysql_query("select * from users_profile where fname like '%$q%'  or     Email like '%$q%' order by rand() LIMIT 10");
         while($row=mysql_fetch_array($sql_res))
  {
      $fname=$row['fname'];
    $lname=$row['lname'];
    $img=$row['profile_pic'];
    $country=$row['country'];
       $uid=$row['uid'];

          $re_fname='<b>'.$q.'</b>';
   $re_lname='<b>'.$q.'</b>';

    $final_fname = str_ireplace($q, $re_fname, $fname);

      $final_lname = str_ireplace($q, $re_lname, $lname);


     ?>

           <div class="display_box" >
      <span><a href="profile.php?f_id=<?php echo $uid;?>"><img src="<?php echo $img; ?>"                width="50" height="50" style="float:left; margin-right:6px" /></a></span>
    <div class="searchtext"><a href="profile.php?f_id=<?php echo $uid;?>"><?php echo             $final_fname; ?>&nbsp;<br/>
    <span style="font-size:10px; color:#999999"><?php echo $country; ?></span></a></div></div>
    <?php
     }
      }
  else
    {
    }
     ?>
$q= trim($_POST['searchword']); //remove whitespaces from front and end