SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Member
    Join Date
    Aug 2000
    Posts
    22
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have written/modified a script that searches a table for a name that user wants to look for. it works. the problem I am having is if there are multiple names that are the same it will only return the first one it finds. what do I need to do to have the script keep searching? here is what I have so far:
    <?php
    require('mydb.inc');

    mysql_connect("localhost",$user,$password);

    $database="employeeprofiles";
    @mysql_select_db("$database") or die( "Unable to select database");

    /*Search database for name*/
    $query="SELECT name,position,extention,phone from employees where name like '%$searchtext%'";

    $result=mysql_query($query);

    mysql_close();

    /*Display Results*/

    $num=mysql_numrows($result);

    if ($num == 0) {
    echo "I'm sorry. There were no matches for that name.";
    }

    else {

    $name = mysql_result($result,0,"name");
    $position=mysql_result($result,0,"position");
    $ext=mysql_result($result,0,"extention");
    $phone=mysql_result($result,0,"phone");


    echo "<tr bgcolor=\"c2ccff\"><td align=middle><b>$name</b></td><td align=middle><b>$position</b></td>";
    echo "<td align=middle><b>$ext</b></td><td align=middle><b>$phone</b>";
    echo "</td></tr></table>";
    }
    ?>



    ------------------
    bl@cky

  2. #2
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here you go:

    <?php
    require('mydb.inc');
    mysql_connect("localhost",$user,$password);

    $database="employeeprofiles";
    @mysql_select_db("$database") or die( "Unable to select database");

    /*Search database for name*/
    $query="SELECT name,position,extention,phone from employees where name like '%$searchtext%'";

    $result=mysql_query($query);

    mysql_close();

    /*Display Results*/

    $num=mysql_numrows($result);

    if ($num == 0) {
    echo "I'm sorry. There were no matches for that name.";
    }

    else {

    while ($row = mysql_fetch_array($result)) {

    echo "<tr bgcolor=\"c2ccff\"><td align=center><b>$row[name]</b></td><td align=center><b>$row[position]</b></td>";
    echo "<td align=center><b>$row[extention]</b></td><td align=center><b>$row[phone]</b>";
    echo "</td></tr></table>";
    }
    }
    ?>

    Should do the trick for you.

    ------------------
    Karl Austin
    KDA Web Services
    "Everyone has a photographic memory. Some just don't have film."


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
  •