skip to Main Content
$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


  1. The query from which you are actually displaying results is this one:

    $query = "SELECT * FROM lastsearches";
    $result = mysqli_query($con, $query);
    

    If you want to limit the results, you need to edit that query instead i.e.

    $query = "SELECT * FROM lastsearches Order By data DESC LIMIT 1";
    $result = mysqli_query($con, $query);
    

    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.

    Login or Signup to reply.
  2. TOP 10 is SQL Server syntax, not MySQL. MySQL uses LIMIT 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?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search