简体   繁体   中英

MySQL select all and start from a specific id

Is there a method that calls all rows into a database and start from a specific id? For example:

I create an images gallery and each images have as "onclick" this function getimage(userid,imageid);

With ajax's json method call every photos by userid but this ever start from the first row. So with second condition "imageid" i could run the animation from that point but i don't know if exist a similiar method.

Condition would be perfect if it were like this:

$query = "SELECT url as href FROM post WHERE user_id = '$user_id' AND type = 'photo' START FROM $imageid";

There is something?

Try this:

$query = "SELECT url as href FROM post WHERE user_id = '$user_id' AND type = 'photo' and image_ID >= $imageid";

Please note that you need to adjust the fieldname to the one mathcing your table.

If you want to get all the images from the database, but start showing them from the provided ID, you'll have to loop through the returned results and store them in a tmp array untill you hit the provided ID. The rest you then add to the output array. in the end, you perform an array_merge to get the first images added to the end of the array.

Use "greater than or equal".

Assuming that the column is image_id and image_id and user_id are integers your query should look like this:

$query = "SELECT url as href FROM post WHERE user_id = $user_id AND type = 'photo' AND image_id >= $imageID";

Be aware that your script is probably wide open to sql-injections if you do not escape the values you are inserting.

I found a good solution for the problem. Basically, I make two arrays with two different query.

  • First one searchs only images with id greater than or equal to image selected.

  • Second one searching for the previuos images.

  • Than i merge two arrays with magical array_merge($firstarray,$secondarray) and add 'cyclic': true to fancybox options.

That's it ;D it works perfecly.

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