August 8th, 2000, 02:41 AM
-
This is a script to search from a form using multiple keywords. The problem with this is that the "query" lines of it do not work so it always comes out with the I'm sorry lines no matter if there are results or not.
Please help, any tips or advice you can give is greatly appreciated!
<HTML>
<HEAD><TITLE>Search Engine Script</TITLE></HEAD>
<?php
$con = mysql_connect ('hostname', 'username', 'password');
mysql_select_db('dbname',$con);
$pieces = explode (" ", $keywords);
$count = count($pieces);
if ($count==1){ $search = $keywords; }
elseif ($count>=1){
for($a=0;$a<$count;$a++){
$query.="%$pieces[$a]%";
if ($a != $count){
$query.=" OR ";
$search = $query;}}}
$sql_result = mysql_query ("Select Article FROM Users WHERE Article = '$search'",$con);
if ($row = mysql_fetch_array($sql_result)){
do {
print "<table><tr>";
print $row[0];
print "</tr><tr>";;
print $row[1];
print "</tr><tr>";
print $row[2];
print "</tr></table>";
} while($row = mysql_fetch_array($sql_result));
}else{
print "I'm sorry, there are no results. <br> Try your search again using more broad keywords.";
?>
August 8th, 2000, 04:41 AM
-
There a pb in the building of your query.
Could you do a test and dump the resulting select statement, then post the query in this forum.
I'll tell you where is the Pb.
Thanks.
JBL
August 8th, 2000, 04:47 AM
-
??
I'm not sure I know what you want me to do, but I did a print query to show errors.
I got this:
"I'm sorry, there are no results.
Try your search again using more broad keywords.%me% OR "
August 8th, 2000, 05:16 AM
-
Just before the line :
$sql_result = mysql_query ("Select Article FROM Users WHERE Article = '$search'",$con);
Do a
echo "search=$search";
and post what you get.
JBL
August 8th, 2000, 05:35 AM
-
<<
<HTML>
<HEAD><TITLE>Search Engine Script</TITLE></HEAD>
<?php
$con = mysql_connect ('hostname', 'username', 'password');
mysql_select_db('dbname',$con);
$pieces = explode (" ", $keywords);
$count = count($pieces);
if ($count==1){ $search = $keywords; }
elseif ($count>=1){
for($a=0;$a<$count;$a++){
$query.="%$pieces[$a]%";
if ($a != $count){
$query.=" OR ";
$search = $query;}}}
$sql_result = mysql_query ("Select Article FROM Users WHERE Article = '$search'",$con);
if ($row = mysql_fetch_array($sql_result)){
do {
print "<table><tr>";
print $row[0];
print "</tr><tr>";;
print $row[1];
print "</tr><tr>";
print $row[2];
print "</tr></table>";
} while($row = mysql_fetch_array($sql_result));
}else{
print "I'm sorry, there are no results. <br> Try your search again using more broad keywords.";
?>
>>
Cinela,
just try the following.
<?php
$con = mysql_connect ('hostname', 'username', 'password');
mysql_select_db('dbname',$con);
$sql="Select Article FROM Users WHERE Article LIKE ";
//your sql statement..
$pieces = explode (" ", $keywords);
$count = count($pieces);
if ($count==1){
$sql.="'%$search%'";
}elseif ($count>=1){
for($a=0;$a<$count;$a++)
{
$sql.="'%$pieces[$a]%'";
if ($a != $count){
$sql.=" OR ";
}
}
}
#print your sql here for debugging purpose
echo $sql;
#sql should be something like "Select Article FROM Users WHERE Article LIKE '%shiju%' OR '%thomas%' OR '%rajan'"; if key word is 'shiju thomas rajan'
$sql_result = mysql_query ($sql,$con);
if ($row = mysql_fetch_array($sql_result)){
do {
print "<table><tr>";
print $row[0];
print "</tr><tr>";;
print $row[1];
print "</tr><tr>";
print $row[2];
print "</tr></table>";
} while($row = mysql_fetch_array($sql_result));
}else{
print "I'm sorry, there are no results. <br> Try your search again using more broad keywords.";
}
?>
------------------
SR -
webshiju.com
"The fear of the LORD is the beginning of knowledge..."
August 9th, 2000, 12:34 AM
-
Well, I did a lot of modification, and I solved my problem.
Thank you for your help. I changed to using like and you gave me an idea for how to fix it, so thanks a lot!
This was my final:
<?php
$con = mysql_connect ('hostname', 'username', 'password');
mysql_select_db('dbname',$con);
$sql="Select Article FROM Users WHERE ";
//your sql statement..
$pieces = explode (" ", $keywords);
$count = count($pieces);
if ($count==1){
$sql.="Article LIKE '$keywords'";
$yellow = $sql;
}elseif ($count>=1){
for($a=0;$a<$count;$a++)
{
$sql.="Username LIKE '%$pieces[$a]%'";
if ($a != $count){
$sql.=" OR ";
$yellow = $sql;
$yellow = substr ($yellow, 0,-3);
}
}
}
$sql_result = mysql_query ("$yellow",$con);
if ($row = mysql_fetch_row ($sql_result)){
do {
print "<table><tr>";
print $row[0];
print "</tr><tr>";;
print $row[1];
print "</tr><tr>";
print $row[2];
print "</tr></table>";
} while($row = mysql_fetch_row($sql_result));
}else{
print "I'm sorry, there are no results. <br> Try your search again using more broad keywords.";
}
?>
August 11th, 2000, 08:16 AM
-
Hello, could you pls post the form stuff you use to search here?
Thnx, i need one myself too
August 13th, 2000, 03:25 AM
-
Well for this one it would just be a basic textbox with the name keywords.
(eg.
<form action="Yourphpscript.php" method=get>
<input type="textbox" maxLength="255" NAME="Keywords" Value="keywords">
</form> )
[This message has been edited by Cinela (edited August 13, 2000).]
October 14th, 2000, 10:43 PM
-
Newbie alert!
I have used the final script and been successful except I a not understanding the following.
$sql="Select Article FROM Users WHERE ";
Am i correct so say that Article is a field in the table named Users ?
After applying my table and field name the program runs but gives me "No records found".
I have forced $keywords to a word I know is in my existing database.
Any suggestions? Thanks.
stujo