Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2008
    Posts
    18

    Unanswered: Problem in search from database

    Hi,
    I have made a search page in which the user enters the keywords to be searched separated with a comma. The code that i ve used works well but it is displaying the name of the user twice.
    Eample: There are users registered in the database. Now if
    A: C,Perl,Python
    B:Java,Perl
    For eg: now the user has entered Perl,Python as the keywords.
    Then it displays the name A twice and B once.
    I wanted that it should display both the name just once.
    I have used the following code:

    PHP Code:
    <?php
    $searches 
    $_SESSION['search'];
    //Explode into seperate keywords and make all unique
    $terms array_unique(explode(',',$searches));
    //Loop through all terms, and add text for query
    foreach($terms as $k=>$v)
    {
        
    //Check for empty values
        
    if($v=="") continue;
        
    $terms[$k]="`TagName` LIKE '%$v%'";
    }
     
    //Implode each term back together with the OR in between them
    $newsearch implode(' OR '$terms);
     
    //Append to query string
    $query1 mysql_query("SELECT TagId FROM tagmaster WHERE ".$newsearch."");
    while(
    $nt=mysql_fetch_array($query1)){
    $query2 mysql_query("SELECT DISTINCT UserName FROM usertags WHERE TagId = '$nt[TagId]'");  
    while(
    $nt1=mysql_fetch_array($query2)){
    echo 
    "{$nt1[UserName]}";
    $result "SELECT FullName,YearsExp FROM userdetails WHERE UserName = '$nt1[UserName]'";
    $data mysql_query($result) or die(mysql_error());
    $resultnum mysql_num_rows($data); 
    if(
    $resultnum>0) { // Echos out matches if anything was found 
    while($info mysql_fetch_array($data)){ 
    ?>
          <tr>
            <td width="139" valign="top"><p align="center"><a href="searchaccount.php?id=<? echo $info['FullName']; ?>" target=\"_blank\"><? echo $info['FullName']; ?></a></p></td>
            <td width="149" valign="top"><p align="center"><? echo $info['YearsExp']; ?></p></td>
          </tr>
    }
    Structure of tables is something like this:

    tagmaster
    4 C
    5 Java
    6 Perl
    7 Python

    usertags
    4 A
    6 A
    7 A
    5 B
    6 B

    userdetails
    A Alvin
    B Babin
    Can you plz tel me what can be possibly wrong in it. I tried using DISTINCT with fullname also but it still dint worked out.

  2. #2
    Join Date
    Apr 2002
    Location
    Toronto, Canada
    Posts
    20,002
    have you ever written a join query?
    Code:
    SELECT DISTINCT
           ud.FullName
         , ud.YearsExp 
      FROM tagmaster AS tm
    INNER
      JOIN usertags AS ut
        ON ut.TagId = tm.TagId
    INNER
      JOIN userdetails AS ud
        ON ud.UserName = ut.UserName
     WHERE tm.TagName LIKE '%Perl%'
        OR tm.TagName LIKE '%Python%'
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL

  3. #3
    Join Date
    Mar 2008
    Posts
    18
    Thanks a lot.
    My problem has got solved.
    Thankyou.

Posting Permissions

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