Advertisement
Guest User

Untitled

a guest
Oct 19th, 2016
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.73 KB | None | 0 0
  1. <?PHP
  2.     function get_random_ids_from_table(&$link, $table, $where = null, $length = -1)
  3.     {
  4.         $ids_r = mysqli_query($link, 'select id from `' . $table . '`' . ($where ? ' where ' . $where : ''));
  5.         if(!$ids_r)
  6.         {
  7.             return array();
  8.         }
  9.  
  10.         $count = mysqli_num_rows($ids_r);
  11.  
  12.         if(!$count)
  13.         {
  14.             return array();
  15.         }
  16.  
  17.         $ids = array();
  18.         if($length < 1 || $length > $count)
  19.         {
  20.             while($id = mysqli_fetch_assoc($ids_r))
  21.             {
  22.                 $ids[] = $id['id'];
  23.             }
  24.             shuffle($ids);
  25.         }
  26.         else
  27.         {
  28.             foreach(array_rand(range(0, $count - 1), $length) as $offset)
  29.             {
  30.                 mysqli_data_seek($ids_r, $offset);
  31.                 $id = mysqli_fetch_assoc($ids_r);
  32.                 $ids[] = $id['id'];
  33.             }
  34.         }
  35.  
  36.         mysqli_free_result($ids_r);
  37.  
  38.         return $ids;
  39.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement