Jump to content

Divide MySQL query result by rows?


grippat
 Share

Recommended Posts

Is there a way to take the result from a MySQL query in PHP and split it up into several results? Say I have a result that contains 20,000 rows. Can I take that and divide it so that one variable holds rows 1 through 10,000 and the other holds rows 10,001 through 20,000?

Link to comment
Share on other sites

There's not an automatic way to do it. You could use the LIMIT clause in the SQL query to only return a certain number of records, like this:SELECT ... FROM table LIMIT 10001, 10000that would get the next 10,000 records starting at record 10,001. Or, to do it yourself you could use something like this to get the first 10,000 in one array, and everything else (if there is) in a second array:

$result = mysql_query(...);$n = 0;$set1 = array();$set2 = array();while ($n++ < 10000 && $row = mysql_fetch_assoc($result))  $set1[] = $row;while ($row = mysql_fetch_assoc($result))  $set2[] = $row;

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...