Jump to content

Recommended Posts

I am trying to write a script that will allow me visitors to my site to search my database for items. I followed a tutorial that I had found and tweaked it to fit my needs. I am not getting any results it keeps telling that there was no search results found. Even though I have the items that I have been testing on in my database.

 

I have two files the main search.php file and the search_func.php file

 

Here is the search.php file

<form method='post' action='search.php'>
<fieldset>
<input type='text' name='search' id='search' value='type name here'/>
<input type='submit' name='search_submit' id='search_submit' value='search'/>
</fieldset>
</form>

<?php

include('includes/search_func.php');

if(isset($_POST['search'])) {
$keywords = mysql_real_escape_string(htmlentities(trim($_POST['search'])));

$errors = array ();

if (empty($keywords )){
$errors[] = 'Please enter a search term';
} else if (strlen($keywords ) < 3 ) {
$errors[] = 'Search term must be more then three characters long';
} else if (search_results($keywords  == false)) {
$errors[] = 'Your search for ' .$keywords . ' showed no results';
}

if (empty($errors)) {
$results = search_results($keywords );
$results_num = count($results);
$suffix = ($results_num != 1) ? 's' : '';
echo 'Your search for <strong>'. $keywords  .'</strong> returned <strong>' .$results_num.'</strong> result'.$suffix.'';

foreach($results as $result) {
	echo $result['first'];
}

} else {
foreach($errors as $error){
echo $error;
}
}

}

?>

 

Here is the search_func.php file

<?php
include('includes/config.php');
connect();

function search_results($keywords ) {
$returned = array();
$where = "";

$keywords  = preg_split('/[\s+]/', $keywords );
$total = count($keywords );

foreach($keywords  as $key=>$keyword) {
	$where .= "`l_name` LIKE '%$keyword%'";

	if($key != ($total - 1)){
		$where .= "OR";
	}
}

$results  = "SELECT `l_name`,`f_name`,`image`,`item_return` FROM `ttmautos` 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(
			'last' => $results_row['l_name'],
			'first' => $results_row['f_name'],
			'image' => $results_row['image'],
			'item' => $results_row['item_return']
		);
	}

	return $returned_results;
}
}
?>

 

If anyone could give me an idea of what I am doing wrong I would appreciate it.

Link to comment
https://forums.phpfreaks.com/topic/254255-not-getting-any-search-results/
Share on other sites

1. This code

	foreach($keywords  as $key=>$keyword) {
	$where .= "`l_name` LIKE '%$keyword%'";

	if($key != ($total - 1)){
		$where .= "OR";
	}

could be changed to one string

$where = implode( ' or ', $keywords );

 

2. Why do you use here

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

one variable $results for different aims? You'd better use 2 different variables.

 

3. Echo your request just when it's created. And show it here - maybe it's wrong? Also you may try to start it in phpmyadmin, it's possible that you really don't have rows in a table that a satisfy your request.

I think I may of found where the problem is in the code but not sure of what I am doing wrong.

 

When I echo out the $results query.

 

$where = "";

$keywords  = preg_split('/[\s+]/', $keywords);
$total = count($keywords );

foreach($keywords as $key=>$keyword) {
	$where .= "`l_name` LIKE '%$keyword%'";
	if($key != ($total - 1)){
		$where .= "OR";
	}
}

$results  = "SELECT * FROM `ttmautos` WHERE $where";
        echo $results;

 

This is what I am getting

SELECT * FROM `ttmautos` WHERE `l_name` LIKE '%%'

 

It worked.. Once I changed this

 

To add to Pikachu comment, look at this line:

} else if (search_results($keywords  == false)) {

Should be:

} else if (!search_results($keywords)) {

 

Regards, PaulRyan.

 

Everything worked fine.

 

Thanks Paul Ryan

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.