简体   繁体   中英

PHP search shows more than one result

I am trying to make a search for my website where if i search with member certificate no it will show his full details. But in my code when i search for Certificate No : 1 it show details of Certificate no 1, 10, 11,12,13,14,15,....

Is there any way to show single member details.

if(isset($_POST['search'])){
  $searchq  = $_POST['search'];
  $searchq = preg_replace("#[^0-9a-z]#i","",$searchq);

  $query = mysql_query("SELECT * FROM  certificate  WHERE certificate_no LIKE '%$searchq%' ") or die ("could not search "); 
}
$count = mysql_num_rows($query);
if($count==0){   
  $output = 'There is no search result ';
} else {
    while ($row = mysql_fetch_array($query)){  
      $name =$row['name']; 
      $certificate_no =$row['certificate_no']; 
      $y_of_passing =$row['y_of _passing'];
      $grade =$row['grade']; 
      $score =$row['score'];

      $output.='<div>Certificate Number : '  .$certificate_no.' <div>Candidate Name :  '.$name. '<div>Grade/Score : '  .$grade.' <div>Course Name :  '.$score. '<div>Year Of Passing: '.$y_of_passing.'<div>'; 

      echo "<img src ='".$row['photo']."' height='100' width='200' >" ; 

   }

}
print("$output");

You're using wrong wildcards Use either = or only like or use RLike

SELECT * FROM  certificate  WHERE certificate_no LIKE '$searchq'
SELECT * FROM  certificate  WHERE certificate_no = '$searchq'
SELECT * FROM  certificate  WHERE certificate_no RLike '[[:<:]]$searchq[[:>:]]'

Change your query:

$query = mysql_query("SELECT * FROM  certificate  WHERE certificate_no LIKE '%$searchq%' ")

to

$query = mysql_query("SELECT * FROM  certificate  WHERE certificate_no  = ". $searchq ."

and try again.

Explanation: When you are using LIKE '%$searchq%' then it will search for a string having 1 at any place ie PATTERN MATCHING . But If you want equality comparison than try = . It will search for exact match.

尝试像这样编辑$ query :::

$query = mysql_query("SELECT * FROM  certificate  WHERE certificate_no LIKE $searchq ") or die ("could not search "); 

Like is a pattern matching . It will search the given string in your column.That's why it's returning matched pattern result like this 10, 11,12,13,14,15 .Did you notice that the result all have that 1 . which is you used to match the pattern in like '%1%' . so you need to use comparison operator like this certificate_no=1

Warning mysql_query, mysql_fetch_array,mysql_connect etc.. extensions were deprecated in PHP 5.5.0, and it was removed in PHP 7.0.0. Instead, the MySQLi or PDO_MySQL extension should be used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM