0

i have a search box script i made. It looks for users with a matching query in the mysql database that a user types in, things like username, location etc.

The script works fine when i have it set to select from one table in my database i.e "ptb_profiles" but when i try and add more tables for it to search from it doesn't like it and lists the same user several times. It should only bring up each user once according to the search query typed by the user.

I already have a limit on the number of search results to save space and allow a user to open more results in a seperate window (that bits not done yet though). Just to be clear thought i don't want to limit the search to one result but to list 5 results and have each result only display once depending on if a user searches display_name under ptb_profiles or nationality under ptb_stats or email under ptb_users.

Can anyone advice me where i'm going wrong in my where clause? thanks.

<form method="get" action="">
<input type="text" name="query" class="search" placeholder="Search Name/Location" style="width:120px;"/>
<input type="image" src="../PTB1/assets/img/icons/loginarrow1.png" class="searchbutton" name="submit" value="Start Search" />
</form>

<?php
//PHP CODE STARTS HERE

if(isset($_GET['submit'])){

// Change the fields below as per the requirements
$db_host="localhost";
$db_username="root";
$db_password="";
$db_name="playtime";
$db_tb_atr_name="display_name";

//Now we are going to write a script that will do search task
// leave the below fields as it is except while loop, which will display results on screen

mysql_connect("$db_host","$db_username","$db_password");
mysql_select_db("$db_name");

$query=mysql_real_escape_string($_GET['query']);

$query_for_result=mysql_query("SELECT *
                        FROM ptb_profiles p, ptb_stats s, ptb_users u
                        WHERE p.display_name like '%".$query."%' 
        OR p.location LIKE '%".$query."%' OR p.hobbies LIKE '%".$query."%' OR s.nationality LIKE '%".$query."%'
        LIMIT 5");
echo "<div class=\"search-results\">";
while($data_fetch=mysql_fetch_array($query_for_result))
{

    echo "<div class=\"spacing\"><a href=\"profile.php?id={$data_fetch['user_id']}\" class=\"search\">";
    echo "<img width=40px height= 40px src=\"data/photos/{$data_fetch['user_id']}/_default.jpg\" class=\"boxgridsearch\"/> "; 
     echo substr($data_fetch[$db_tb_atr_name], 0,160);
    echo "</a></div>";

}
echo "<div class=\"morebutton-search\"><a href=\"echo '%".$query."%'\">+ view more results</a></div>";




mysql_close();
}

?>

p.s if anyone knows how i can make a hyperlink to take a user to a new page listing their current search query in a new window but obviously this time around display more than 5 and display all the results for that search i would be really greatful.

i have tried this but it's more than likely completely wrong because it's not working:

echo "<div class=\"morebutton-search\"><a href=\"echo '%".$query."%'\">+ view more results</a>
2

3 Answers 3

1

What you are doing is a cross join. In that, all rows in The profiles table, stats table and users table are combined into one table with all the various combinations. Hence you are getting the same user multiple times.

what you need is a "inner join" or some connection between the tables to narrow down your select.

if you DO NEED the "cross join", then use the DISTINCT key word as suggested by MAXIM.

link to the above suggestions: http://www.tizag.com/mysqlTutorial/mysqljoins.php

0

Try changing your mySQL quesry to this:

$query_for_result=mysql_query("SELECT DISTINCT *
                    FROM ptb_profiles p, ptb_stats s, ptb_users u
                    WHERE p.display_name like '%".$query."%' 
    OR p.location LIKE '%".$query."%' OR p.hobbies LIKE '%".$query."%' OR s.nationality LIKE '%".$query."%'
    LIMIT 5");

notice the DISTINCT statement. It will not duplicate you the rows.

1
  • that gives me this error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in /Applications/XAMPP/xamppfiles/htdocs/PTB1/includes/mod_sidebar/search.php on line 34 Commented Dec 10, 2012 at 20:01
0

Assuming that all tables have a user_id field and that there is exactly one row in each table for a given user then you can use inner joins as suggested by Bharath Parlapalli:

SELECT *
FROM ptb_profiles p
INNER JOIN ptb_stats s ON (p.user_id = s.user_id)
INNER JOIN ptb_users u ON (u.user_id = s.user_id)
WHERE p.display_name like 'foo' 
    OR p.location LIKE 'foo' 
    OR p.hobbies LIKE 'foo' 
    OR s.nationality LIKE 'foo'
LIMIT 5

As to your second question you can do something like:

<div class=\"morebutton-search\"><a href="mypage.php?query=<?php echo $query;?>&start=x">+ view more results</a>

Then use $_GET["start"] to find out which set of results to get.

Also don't use $query without properly sanitisng it or, even better, using prepared statements. Since $query comes from the user it could have dangerous content such as:

"'DROP TABLE foo;--"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.