If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Problem in search from database

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-28-08, 15:04
agarwalsrushti agarwalsrushti is offline
Registered User
 
Join Date: Mar 2008
Posts: 18
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.
Reply With Quote
  #2 (permalink)  
Old 03-28-08, 15:13
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
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%'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 03-28-08, 15:27
agarwalsrushti agarwalsrushti is offline
Registered User
 
Join Date: Mar 2008
Posts: 18
Thanks a lot.
My problem has got solved.
Thankyou.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On