Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    Question Searching mysql with php help!

    Hello,
    I'm trying to search mysql with php,
    and have one or two problems,

    my code:
    PHP Code:

    <?php
    include 'admin/conn.php';
    $link mysql_connect($SqlHost$SqlUser$SqlPass) or die('Login details incorrect');
    $db_selected mysql_select_db($SqlDb$link) or die('Database name incorrect');

    $search $_REQUEST['search'];
    $searchdrop $_REQUEST['searchdrop'];

    //Count Search Results
    Function SearchCount($search,$searchdrop)
    {

    global 
    $SqlHost;
    global 
    $SqlUser;
    global 
    $SqlPass;
    global 
    $SqlDb;


    $link mysql_connect($SqlHost$SqlUser$SqlPass) or die('Login details incorrect');
    $db_selected mysql_select_db($SqlDb$link) or die('Database name incorrect');
    $query "SELECT * FROM listings WHERE keywords LIKE \"%$search%\" AND status=2 AND cid=$searchdrop";
    $numguests mysql_query($query) or die("Select Failed!");
    $numguest mysql_fetch_array($numguests);

    echo 
    "$numguest[0]";

    }

    echo 
    "search results for \"<b>$search</b>\" returned ";
    SearchCount("$search","$searchdrop");
    echo 
    " matches<br /><br />";


    $result mysql_query("SELECT * FROM listings WHERE keywords LIKE \"%$search%\" AND status=2 AND cid=$searchdrop");

    while (
    $row3 mysql_fetch_assoc($result))
    {
    echo 
    "<table cellpadding=2 cellspacing=1 border=0 width=400 bgcolor='#E6E6E6'>";
    echo 
    "<tr><td bgcolor='#FF9E21' align=left><a href='$row3[link]' target='_blank'>$row3[Name]</td></tr>";
    echo 
    "<tr><td align=left>$row3[Description]</td></tr>";
    echo 
    "</table><br />";
    }

    ?>
    first problem,
    my function:
    Function SearchCount($search,$searchdrop)
    to count the number of records found, gives me 9 for some reason, and only one record is being displayed using the same select query as you can see, or maybe I'm just not seeing something.

    also if I just click on the search button on my search form, it returns everything in the mysql table i'm searching, why does it do this?

    thanks in advance for your time!
    -Ken

  • #2
    Regular Coder devinemke's Avatar
    Join Date
    Dec 2004
    Location
    NYC
    Posts
    443
    Thanks
    0
    Thanked 12 Times in 11 Posts
    PHP Code:
    <?php
    include('admin/conn.php');
    mysql_connect($SqlHost$SqlUser$SqlPass) or exit(mysql_error());
    mysql_select_db($SqlDb$link) or exit(mysql_error());

    $search $_REQUEST['search']; 
    $searchdrop $_REQUEST['searchdrop']; 
     
    $sql "SELECT * FROM listings WHERE keywords LIKE '%$search%' AND status = '2' AND cid = '$searchdrop'"
    $result mysql_query($sql) or exit(mysql_error());
    $num_rows mysql_num_rows($result); 

    echo 
    'search results for <b>' $search '</b> returned ' $num_rows ' matches<br /><br />';

    if (
    $num_rows)
    {
        echo 
    '<table cellpadding="2" cellspacing="1" border="0" width="400" bgcolor="#E6E6E6">';
        while (
    $row mysql_fetch_assoc($result))
        {
            echo 
    '
            <tr><td bgcolor="#FF9E21" align="left"><a href="' 
    $row['link'] . '" target="_blank">' $row['Name'] . '</td></tr>
            <tr><td align="left">' 
    $row['Description'] . '</td></tr>
            '
    ;
            
        }
        echo 
    '</table>';
    }
    ?>

  • #3
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ah kool!

    thanks!
    -Ken


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •