Jump to content

Search Multiple Rows


justlukeyou

Recommended Posts

Hi,

 

I have search script which will only search one row at a time. The first two codes below work however the third fails to work but I am using the same code to query the two rows for the results string.

 

Can anyone advise how search more than one row at a time?

 

 

This searches firstname

$where .= "`surname` LIKE '%$keyword%'";

 

This searches surname

$where .= "`surname` LIKE '%$keyword%'";

 

This search fails

$where .= "`firstname`, `surname` LIKE '%$keyword%'";

 

 

 

<?php
function search_results ($keywords) {
$returned_results = array();
$where = "";
$keywords = preg_split('/[\s]+/', $keywords);
$total_keywords = count($keywords);
foreach($keywords as $key=>$keyword) {
$where .= "`surname` LIKE '%$keyword%'";
if ($key != ($total_keywords - 1)) {
$where .= " AND ";
}
}
$results = "SELECT `firstname`, `surname` FROM `users` WHERE $where";

$results_num = ($results = mysql_query($results)) ? mysql_num_rows($results) : 0;

if ($results_num === 0) {
return false;
}else{
while ($results_row = mysql_fetch_assoc($results)) {
$returned_results[] = array(
			    'firstname' => $results_row['firstname'],
			    'surname' => $results_row['surname'],	
);

Link to comment
https://forums.phpfreaks.com/topic/272242-search-multiple-rows/
Share on other sites

You would check both. If you wanted to look for someone named James Howard, you could do ..

 

`firstname` = 'James' AND `surname` = 'Howard'

 

To search from the keyword, you would change the = '' to LIKE '%{$keyword}%' like you're doing. You just have to compare both, you don't choose two fields at the same time for comparing.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.