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>
mysql_*
functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.