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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.