$dbhost = "localhost";
$dbuser = "root";
$dbpass = "";
$dbname = "igscript";
$con = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);
$query = "SELECT * FROM lastsearches";
$result = mysqli_query($con, $query);
if(mysqli_connect_errno()) {
die("DB Error: " . mysqli_connect_error() . " ( " .mysqli_connect_errno() . ")");
}
while($row = mysqli_fetch_assoc($result)) {
$query = "SELECT * FROM lastsearches Order By data DESC LIMIT 1;";
echo '<center><p>'.$row["name"].'</p> </center><hr>';
if(!mysqli_query($con, $query)) {
die('Error : '.mysqli_error($con));
}
}
$result = mysqli_query($con, $query);
Whenever i use either LIMIT 1, or LIMIT 10; at $query, it has no effect at all. Still displays the same amount of rows. I tried also TOP 10 or TOP(10) as I seen on internet, and i’m getting
Error : You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ’10 name FROM lastsearches Order By data DESC LIMIT 1′ at line 1
$query = "SELECT TOP 10 name FROM lastsearches Order By data DESC";
-> this was the query;
Also the first query worked properly in phpmyadmin, section SQL.
2
Answers
The query from which you are actually displaying results is this one:
If you want to limit the results, you need to edit that query instead i.e.
I’m not sure what you are trying to achieve with the query in your while loop, but it is not doing anything inside that loop so you can probably remove it.
TOP 10
is SQL Server syntax, not MySQL. MySQL usesLIMIT 10
with a very similar effect.Since I don’t see
TOP 10
in your code, could it be some interface package that is tied to SQL Server?