0

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 INSERTs and UPDATEs)

As printing the results can take a very long time (e.g. 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?

s3lph
  • 4,575
  • 4
  • 21
  • 38
  • I think the easiest way to do this is have a config file entry that defines your limit and then pass that variable into your query every time through the LIMIT property. I don't see anything wrong with this approach either. – ApperleyA Mar 25 '15 at 21:30

3 Answers3

1

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.

D3F
  • 156
  • 1
  • 9
0

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

Edwin Krause
  • 1,766
  • 1
  • 16
  • 33
0

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

Let us know if this helps.

Tom
  • 432
  • 2
  • 9