how to remove duplicate results from mysql query in php? - php

I'm trying to provide a search function for my PHP site. The users should be able to search rows and columns for their desired query, like "search engine". I tried this php code:
<?php
$con = #mysqli_connect("localhost", "root", "", "search");
$output = '';
if(isset($_POST['search'])) {
$search = $_POST['search'];
$search = preg_replace("#[^0-9a-z]i#","", $search);
$query = mysqli_query($con, "SELECT * FROM sites WHERE (site_title LIKE '%$search%' or site_link LIKE '%$search%' or site_keywords LIKE '%$search%')") or die ("Could not search");
$count = mysqli_num_rows($query);
if($count == 0){
$output = "There was no search results!";
print $output;
}else{
while ($row = mysqli_fetch_array($query)) {
$site_keywords = $row ['site_keywords'];
$site_tit = $row ['site_title'];
$site_link = $row ['site_link'];
$output .='<div> '.$site_tit.''.$site_keywords.''.$site_link.'</div>';
print $output;
}
}
}
?>
Everything works just fine but I'm getting duplicate results. I've read a lot of answers and here's I've done so far: I used SELECT DISTINCT * FROM .... and also SELECT DISTINCT site_id FROM .... but didn't return any result. I tries GROUP BY but they didn't remove the duplicates and returned nothing. I also applied PHP array_unique() on $row = mysqli_fetch_array($query) in where condition, but it also didn't return any result.
If I can do this by using only SQL please or I have to remove duplicates by PHP like using a function, please guide me. Thanks in advance.

Move:
print $output;
outside the loop.
Right now $output is being printed every time through the loop. If your results are A,B,C then your output will be A,A,B,A,B,C (with divs, etc.)

Related

Getting multiple results without query inside loop

I'm trying to improve the loading times of images and need to change around the code.
I haven't had any luck finding out how to do this and I'm not sure if it is even possible. In the example below you can see that I use KEY to match it with U_KEY to get FILE_PATH which I then add to a long comma delimited $allPaths string.
I know I should not use queries inside loops but I have no idea how to change this.
<?php
$sql = "SELECT * FROM test_users, image_uploads LIMIT 0, 27";
$result = mysqli_query($mysqli, $sql);
while($value = mysqli_fetch_array($result)) {
$files_key = $value["KEY"];
$allPaths = "";
$inner_query = "SELECT * FROM additional_uploads WHERE U_KEY = '".$files_key;
$inner_result = mysqli_query($mysqli, $inner_query);
while ($row = mysqli_fetch_array($inner_result)) {
$allpaths .= $row['FILE_PATH'].",";
}
// how do I get $allPaths without using a query inside the while loop?
}
?>
If someone could tell me how I can get $allPaths with only one query instead of multiple queries inside the loop as shown above it would probably load the images much faster.
Is this possible?
Edit
I have tried to understand the problem using the suggested answer and additionally I was looking on other forums as well to find out more. However I still cannot find a solution. Since I'm using mysqli_fetch_array the suggested answer really confuses me even further.
<?php
$inner_query = "SELECT * FROM additional_uploads";
$inner_result = mysqli_query($mysqli, $inner_query);
$rows = [];
while ($row = mysqli_fetch_array($inner_result)) {
$rows[$row['U_KEY']] .= $row['FILE_PATH'].",";
}
$sql = "SELECT * FROM test_users, image_uploads LIMIT 0, 27";
$result = mysqli_query($mysqli, $sql);
while($value = mysqli_fetch_array($result)) {
$files_key = $value["KEY"];
$allPaths = $rows[$files_key];
}
?>

How to write a query for autocomplete in mysqli?

code:
<?php
include('config.php');
$return_arr = array();
$term = $_GET['term'];
$term = str_replace('.','',$term);
$sql = "SELECT * FROM submission where keyword like '".$term."%' or keyword
like '%".$term."%' ORDER BY CASE WHEN keyword LIKE '".$term."%' THEN 1
ELSE 2 END";
$r = mysqli_query($link,$sql);
while($row = mysqli_fetch_assoc($r))
{
$key = explode(",", $row['keyword']);
foreach ($key as $keyword)
{
$return_arr[] = $keyword;
}
}
echo json_encode($return_arr);
?>
I have created autocomplete suggestion box and its working properly but I have a column name keyword where my data is look line apolo,alto,bmw,camaro,ducati like this where I am using expload function for listing one by one. If I search with (a) alphabet it showing a result but when I want to search with (b) alphabet its show nothing. So, How can I remove this problem ?Please help me.
Thank You
Use the IN filter to get better results for multiple keywords
SELECT keyword FROM submission WHERE keyword IN ('Apolo', 'BMW', ...);

Why I can not echo mysql query result when using WHERE statement

I have the next code which shows no results:
<?php include ("access.php");
$corpustitle = "Korpus Bilingüe Alemany-Català (de)";
$result = mysqli_query($dbiac, "SELECT corpus FROM corpus_info WHERE title = '$corpustitle'") or die(mysqli_error($dbiac));
while($row = mysqli_fetch_array($result)){
echo $row['corpus']."<br>";
} ?>
But if I take the WHERE statement out of the query I get next results:
$result = mysqli_query($dbiac, "SELECT corpus FROM corpus_info") or die(mysqli_error($dbiac));
banctraddeucat_ca
banctraddeucat_de
banctraddeuspa_de
...etc.
And also if I do the original query at the phpmyadmin I get the result I'm looking for:
Yes, after #delboy1978uk comments I added this line before the query, and that made the difference:
mysqli_set_charset($dbiac, 'utf8');
So the entire code now is:
<?php include ("access.php");
$corpustitle = "Korpus Bilingüe Alemany-Català (de)";
mysqli_set_charset($dbiac, 'utf8');
$result = mysqli_query($dbiac, "SELECT corpus FROM corpus_info WHERE title = '$corpustitle'") or die(mysqli_error($dbiac));
while($row = mysqli_fetch_array($result)){
echo $row['corpus']."<br>";
} ?>
Thanks...

How can I get this php to return the entire column of an sql db

I am trying to query a db for an entire column of data, but can't seem to get back more than the first row.
What I have so far is:
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_fetch_array($medicationItemObj, MYSQLI_NUM)){
echo count($row);
}
It's not my intention to just get the number of rows, I just have that there to see how many it was returning and it kept spitting out 1.
When I run the sql at cmd line I get back the full result. 6 items from 6 individual rows. Is mysqli_fetch_array() not designed to do this?
Well, I had a hard time understanding your question but i guess you are looking for this.
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
if($row = mysqli_num_rows($medicationItemObj))
{
echo $row;
}
Or
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
$i = 0;
while ($row = mysqli_fetch_array($medicationItemObj))
{
$medicationItem[] = $row[0];
$i++;
}
echo "Number of Rows: " . $i;
If you just want the number of rows i would suggest using the first method.
http://php.net/manual/en/mysqli-result.num-rows.php
You can wrote your code like below
$medicationItem = array();
$medicationItemSql = "SELECT medication FROM medication";
$medicationItemObj = mysqli_query($connection, $medicationItemSql);
while ($row = mysqli_fetch_assoc($medicationItemObj))
{
echo $row['medication'];
}
I think this you want
You could give this a try:
$results = mysqli_fetch_all($medicationItemObj, MYSQLI_NUM);
First, I would use the object oriented version of this and always use prepared statements!
//prepare SELECT statement
$medicationItemSQL=$connection->prepare("SELECT medication FROM medication");
// execute statement
$medicationItemSQL->execute();
//bind results to a variable
$medicationItemSQL->bind_result($medication);
//fetch data
$medicationItemSQL->fetch();
//close statement
$medicationItemSQL->close();
You can use mysqli_fetch_assoc() as below.
while ($row = mysqli_fetch_assoc($medicationItemObj)) {
echo $row['medication'];
}

PHP - Why is looping between WHILE and DO-WHILE give different results in mysql fetching?

I have one record in a table in my mysql database. I use the following PHP code to retrieve data:
$result = mysql_query($query = "SELECT realname FROM t_user");
if($result)
{
while($data=mysql_fetch_assoc($result)){
echo $data['realname'];
}
}
the results do not appear, but when I use a do-while loop like below:
if($result)
{
$data=mysql_fetch_assoc($result);
do{
echo $data['realname'];
} while($data=mysql_fetch_assoc($result));
}
the results appear, then I tried to add one more record to the table, in the while loop, only shows one data record (the first record), and the do-while loop displays all the data. Why is that? Is it because there is my code wrong?
The code you have shown can't possibly exhibit this problem, so the logical explanation is this:
$result = mysql_query($query = "SELECT realname FROM t_user");
if($result)
{
// Something fetched a row from $result before this statement is run
while($data=mysql_fetch_assoc($result)){
echo $data['realname'];
}
}
Seeing how you're comparing two similar codes, you may have accidentally written something in between the two:
if($result)
{
mysql_fetch_assoc($result);
while($data=mysql_fetch_assoc($result)){
echo $data['realname'];
}
}
$result = mysql_query($query = "SELECT realname FROM t_user");
if($result) { while($data=mysql_fetch_assoc($result)){
echo $data["realname"]; } }
$result = mysql_query($query = "SELECT realname FROM t_user");
var_dump($data);die;
see the result array in your browser. the above while loop should work. check your result.

Categories