SitePoint Sponsor

User Tag List

Results 1 to 7 of 7

Thread: Searching mysql

  1. #1
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Searching mysql

    Hi,

    Im trying to search mysql table and echo the matching results. Below is my script .. when I search it never brings back any records .. just shows the form a again .. How exactly is it done?

    PHP Code:
    <form name="form" method="post" action="search.php">
    Search: <input name="search" type="text" size="25">
    <input type="submit" name="Submit" value="Submit"></form>

    <?php

    include("db.php");

    if(isset(
    $_POST['post'])) {

    $search $_POST['search'];

    $sql "SELECT user_name, email FROM users WHERE (user_name LIKE('$search%') OR email LIKE('%$search%'))";
    $result mysql_query($sql);
            while (
    $row mysql_fetch_array($result)) {
            
    $user_name $row['user_name'];
            
    $email_address $row['email_adddress'];

        if (!
    $result) {

        echo 
    "<b>Error!</b> Please contact the administrator.";
        return;
        
    } else {

        echo 
    "$user_name -- $email_address";
        return;
        
      }
     }
    }

    ?>

    Cheers

  2. #2
    SitePoint Zealot metho's Avatar
    Join Date
    Feb 2005
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "SELECT user_name, email_address FROM users WHERE (user_name LIKE('$search%') OR email LIKE('%$search%'))";
    $result = mysql_query($sql);
    while ($row = mysql_fetch_array($result)) {
    $user_name = $row['user_name'];
    $email_address = $row['email_adddress'];

    Your email column name is inconsistent..

  3. #3
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    True, but thats not whats stoping it from matching any records?

    Cheers

  4. #4
    SitePoint Zealot metho's Avatar
    Join Date
    Feb 2005
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "SELECT user_name, email_address FROM users WHERE (user_name LIKE('%$searchUser%') OR email_address LIKE('%$searchEmail%'))";

    check that where clause dude...

    use %$string for $string at end of value, $string% for string at beginning of value or %$string% anywhere in value. I recon %$string% will give better results. Your code is sound, but just make the where clause more flexible...

  5. #5
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    So it seems is should be working?

    If so how come it never echoes any results .. ?

    Cheers

  6. #6
    SitePoint Zealot metho's Avatar
    Join Date
    Feb 2005
    Posts
    132
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this dude:

    Code:
    <?php
    include("db.php");
    if(isset($_POST['search'])) {
    $search = $_POST['search'];
    $sql = "SELECT user_name, email_address FROM users WHERE user_name LIKE '%".$search."%' OR email_address LIKE '%".$search."%'";
    $result = mysql_query($sql);
            $user_name = $row['user_name'];
            $email_address = $row['email_adddress'];
    		$row = mysql_fetch_assoc($result);
    //
        if (!$result) {
        echo "<b>Error!</b> Please contact the administrator.";
    	} else {
    		$i=0;
    		do{
    		$i++;
        	echo 'Result '.$i.': '.$row['user_name']." -- ".$row['email_adddress']."<br />";
    		} while($row = mysql_fetch_assoc($result));
    	}
    }
    ?> 
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Search Form</title>
    </head>
    <body>
    <form name="form" method="post" action="#">
    Search: <input name="search" type="text" size="25">
    <input type="Submit" name="Submit" value="Go">
    </form>
    </body>
    </html>
    dont forget to include the second param in mysql_query($sql) to ensure the database is being connected to...
    Last edited by metho; May 25, 2005 at 18:24.

  7. #7
    SitePoint Guru
    Join Date
    Aug 2004
    Location
    Earth
    Posts
    739
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    Havnt tried your above code .. But the following seems to work by switching:

    if(isset($_POST['post'])) {

    to

    if(isset($_POST['search'])) {

    It shows all records .. once search is sbmited, it returns the search results.

    How do I make it so if it returns no records .. have it echo 'No records matching your search string';?

    PHP Code:
    <?php

    include("db.php");

    if(isset(
    $_POST['search'])) {

    $search $_POST['search'];

    $sql "SELECT user_name, email_address FROM users WHERE (user_name LIKE('%$search%') OR email_address LIKE('%$search%'))";

    } else 

    $sql "SELECT user_name, email_address FROM users";

    $result mysql_query($sql);
            while (
    $row mysql_fetch_array($result)) {
            
    $user_name $row['user_name'];
            
    $email_address $row['email_address'];

        if (!
    $result) {

        echo 
    "<b>Error!</b> Please contact the administrator.";
        
    } else {

        echo 
    "$user_name -- $email_address<br>";
        
      }
     }

    ?>

    <form name="form" method="post" action="search.php">
    Search: <input name="search" type="text" size="25">
    <input type="submit" name="Submit" value="Submit"></form>
    Cheers
    Last edited by _matrix_; May 25, 2005 at 20:56.


Bookmarks

Posting Permissions

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