Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?PHP
- function get_random_ids_from_table(&$link, $table, $where = null, $length = -1)
- {
- $ids_r = mysqli_query($link, 'select id from `' . $table . '`' . ($where ? ' where ' . $where : ''));
- if(!$ids_r)
- {
- return array();
- }
- $count = mysqli_num_rows($ids_r);
- if(!$count)
- {
- return array();
- }
- $ids = array();
- if($length < 1 || $length > $count)
- {
- while($id = mysqli_fetch_assoc($ids_r))
- {
- $ids[] = $id['id'];
- }
- shuffle($ids);
- }
- else
- {
- foreach(array_rand(range(0, $count - 1), $length) as $offset)
- {
- mysqli_data_seek($ids_r, $offset);
- $id = mysqli_fetch_assoc($ids_r);
- $ids[] = $id['id'];
- }
- }
- mysqli_free_result($ids_r);
- return $ids;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement