Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

search database script problems

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
Hi, I was wondering if you could help me with the following code. It's carried out from a form where the user inputs the text to search in an input box called "search" and there's two options to search by "name" or "description" which are radio buttons with the name request and the values "name" and "description" respectively.

The variable $limit is set to 15 through a config file. It's not my original code so I've tried editing it with no success. My trouble was that I don't know what the variable $operateur comes from or what it does.

The unexpected results i'm receiving is that some times when you search it just comes up blank when correct informatio is inputted. The limit links don't appear either so It only displays 15 results. I would also like it to arrange the results by the name. You can test the script at
I'd be very greatful for your help. Thanx

<?

$db = mysql_connect($host,$login,$pass);
mysql_select_db($base,$db);

if (!isset($debut)) $debut = 0;
if (!empty($search))
{
$search=strtolower($search);
$mots = str_replace( &quot;+&quot;, &quot; &quot;, trim($search));
$mots = str_replace( &quot;\&quot;&quot;, &quot; &quot;, $mots);
$mots = str_replace( &quot;,&quot;, &quot; &quot;, $mots);
$mots = str_replace( &quot;:&quot;, &quot; &quot;, $mots);
$search=rawurlencode($search);

$tab=explode( &quot; &quot; , $mots);
$nb=count($tab);

$sql= &quot;SELECT * FROM bands_tbl WHERE 1 and $request like \&quot;%$tab[0]%\&quot; &quot;;

for($i=1 ; $i<$nb; $i++)
{
$sql.= &quot;$operateur $request like \&quot;%$tab[$i]%\&quot; &quot;;
}

$sql2=$sql;
$sql.= &quot; Limit $debut,$limit &quot;;

$result2 = mysql_db_query($base,$sql2);
$result = mysql_db_query($base,$sql);

if($result)
{
$nrows = mysql_num_rows($result2);
$flag = 1;
if(mysql_num_rows($result)==0) echo '<center><font face=&quot;Verdana&quot; size=&quot;2&quot;><b>No result</b></font></center>';
else
{
$nb=1;
while($data = mysql_fetch_array($result)) {
echo '<p align=&quot;left&quot; style=&quot;margin-bottom: 5&quot;><font face=&quot;Verdana&quot; size=&quot;2&quot;><img border=&quot;0&quot; src=&quot;../images/lnav_5.gif&quot; width=&quot;12&quot; height=&quot;8&quot;><a href=&quot;'.$data['site'].'&quot; target=&quot;_blank&quot;>
<b>'.$data['name'].'</b></a></font></b></p>';
}

$nombre=ceil($nb/$limit);

echo &quot;<center>&quot;; if($debut>0)
{
echo &quot;<font face=\&quot;Verdana\&quot; size=\&quot;1\&quot;><a href=search.php?search=$search&operateur=$operateur&debut=&quot;.($debut-$limit). &quot;><<</a> </font> &quot;;
}

if ($nombre>1)
{ for($i=1; $i<=$nombre; $i++)
{
echo &quot;<font face=\&quot;Verdana\&quot; size=\&quot;1\&quot;><a href=search.php?search=$search&operateur=$operateur&debut=&quot;.(($i-1)*$limit). &quot;>&quot;.$i. &quot;</a> </font> &quot;;
}
}
if(($debut+$limit)<$nb)
{
echo &quot;<font face=\&quot;Verdana\&quot; size=\&quot;1\&quot;><a href=search.php?search=$search&operateur=$operateur&debut=&quot;.($debut+$limit). &quot;>>></a></font>&quot;;
}

echo &quot;</CENTER>&quot;;

}

}
}

else
{
echo( &quot;<center><br><b><font face=\&quot;Verdana\&quot; size=\&quot;2\&quot;>You have to enter at least one word</font></b></center>&quot;);
}

mysql_close($db);

?>
 
well this is my first time actually posting here even tho i've been reading the posts here for awhile now... so i guess its my time to give beck to the community =)

from what i can c the sql statement is where some of the problem is:
$sql= &quot;SELECT * FROM bands_tbl WHERE 1 and $request like \&quot;%$tab[0]%\&quot; &quot;;

what does &quot;WHERE 1&quot; mean??? i dont think i understand that part of the statement...

it should read something like:
#check what ur searching for
if ($request == &quot;name&quot;)
{
$searchplace = &quot;name&quot;;
}
else
{
$searchplace = &quot;desciption&quot;;
}

$operateur = &quot;AND&quot;;
$sql = &quot;SELECT * from bands_tbl WHERE&quot;;

for($i=0 ; $i<$nb; $i++)
{
$sql.= &quot;$operateur $searchplace like \&quot;%$tab[$i]%\&quot; &quot;;
}

$sql2=$sql;
$sql.= &quot; Limit $debut,$limit &quot;;

not sure if any of that helps u at all... i'm not really used to mysql, i mainly work with postgresql but sql is still pretty much sql

good luck
-jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top