简体   繁体   中英

Limit results of all MySQL queries

I wrote a PHP/MySQLi frontend, in which the user can enter SQL queries, and the server then returns the results in a table (or prints OK on INSERT s and UPDATE s)

As printing the results can take a very long time (eg SELECT * FROM movies ) in a IMDb extract with about 1.6M movies, 1.9M actors and 3.2M keywords, I limited the output to 50 rows by cancelling the printing for-loop after 50 iterations.

However, the queries themselves also take quite some time, so I hoped that it might be possible to set a global maximum row return value, nevertheless whether the LIMIT keyword is used or not. I only intended to use the server for my own practice, but as some people in my class are struggling with the frontend provided by the teacher (Windows EXE, but half of the class uses Mac/Linux), I decided to make it accessible to them, too. But I want to keep my Debian VM from crashing because of - well, basically it would be a DDoS.

For clarification (examples with a global limit of 50):

SELECT * FROM movies;
> First 50 rows
SELECT * FROM movies LIMIT 10;
> First 10 rows
SELECT * FROM movies LIMIT 50,100;
> 50 rows (from 50 to 99)

Is there any possibility to limit the number of returned values using either PHP/MySQLi or the MySQL server itself? Or would I have to append/replace LIMIT to/in the queries?

You can use there queries and add "LIMIT 50" to it.

And if they added LIMIT by them self just filter it out with regex and still add your LIMIT.

I believe you have to build yourself a paginator anyway, avoiding to use LIMIT statement is not really possible i believe.

Here is what I would suggest for a Paginator:

if($_REQUEST['page'] == ""){
 $page = 1;
}else{
 $page = $_REQUEST['page']; // perhaps double check if numeric
}

$perpage = 50;
$start = ($page - 1) * $perpage;

$limit_string = " LIMIT ". $start . "," . $perpage ;

$query = "SELECT * FROM movies";
$query .= $limit_string;

Hope that helps

You can create a function. https://dev.mysql.com/doc/refman/5.0/en/create-function.html

Let us know if this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM