#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    17
    Rep Power
    0
    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.";
    ?>
  2. #2
  3. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    France
    Posts
    16
    Rep Power
    0
    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
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    17
    Rep Power
    0
    ??
    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 "
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Location
    France
    Posts
    16
    Rep Power
    0
    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
  8. #5
  9. .Net Developer
    Devshed Novice (500 - 999 posts)

    Join Date
    Feb 2000
    Location
    London
    Posts
    987
    Rep Power
    15

    <<
    <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..."
  10. #6
  11. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    17
    Rep Power
    0
    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.";
    }
    ?>

  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    2
    Rep Power
    0
    Hello, could you pls post the form stuff you use to search here?

    Thnx, i need one myself too
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2000
    Posts
    17
    Rep Power
    0
    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).]
  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2000
    Posts
    61
    Rep Power
    14
    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

Similar Threads

  1. Replies: 0
    Last Post: February 9th, 2004, 04:33 PM
  2. Problems implementing search
    By donnie_darko in forum PHP Development
    Replies: 5
    Last Post: February 4th, 2004, 02:32 AM
  3. Problems with 'search here"!
    By SailFan in forum Dev Shed Lounge
    Replies: 1
    Last Post: January 26th, 2004, 07:55 PM
  4. Cgi Search feature script
    By butsy_7 in forum Perl Programming
    Replies: 5
    Last Post: January 22nd, 2004, 07:43 PM
  5. login script problems
    By harwoodspike in forum PHP Development
    Replies: 2
    Last Post: January 16th, 2004, 08:55 PM

IMN logo majestic logo threadwatch logo seochat tools logo