Generate a 5 digit number, but with exception from a MySQL table column -
i have mysql database contains table named images
. in table have column named file_name
. file_name
column contains 5 rows:
.---.-----------. | # | file_name | .---.-----------. | 1 | 00000 | | 2 | 00001 | | 3 | 00002 | | 4 | 00003 | | 5 | 00004 | .---------------.
my query code is:
<?php require_once('config/config_db.php'); $sqlquery = "select `file_name` images"; $result = mysql_query($sqlquery); while($row = mysql_fetch_array($result)){ echo $row['file_name']."<br>"; // ? } ?>
i have next php function generates 5 digit random function, exeptions introduced manually in $exception
variable array (as can see below). goal file_name
column rows, , set in $exception
variable. tried accomplish 3 or 4 hours, no success. give thanks time. the php function:
<?php function getrandom($exception=array('00000', '00001', '00002', '00003', '00004')){ // ? $rand = str_pad(mt_rand(0,99999),5,0,str_pad_left); if(in_array($rand,$exception))return getrandom($exception); homecoming $rand; } echo getrandom().'<br>'; ?>
based on before question, might job in single sql statement:-
select anumber ( select lpad(convert(a.i+b.i*10+c.i*100+d.i*1000+e.i*10000,char(5)),5,'0') anumber integers a, integers b, integers c, integers d, integers e) sub1 left outer bring together images b on sub1.anumber = b.file_name b.file_name null order rand() limit 1
uses table called integers single column called , 10 rows values 0 9.
it using order rand() bit slow worth optimising bit.
mysql table exception numbers
No comments:
Post a Comment