Sagewire Logo

control structure for a falied MySQL query?

2 Message(s) by 2 Author(s) originally posted in php general


From: lawpoop Date:   Wednesday, October 24, 2007
Hello all -

I have an SQL statement that generate s a random code number. In the
table definition, the code number has a unique index, so my concern is
that the randomly-generated number could fail the UNIQUE constraint.

What PHP control structure can I use to keep re-trying a query until
it does not fail? Most of the time it'll successfully insert, and my
guess is that it'll only need a second try to generate an unused
number. But, I'd like to have bullet-proof code to keep trying the
statement until it does not fail.

How could I do this? Here is my sql statement:

INSERT INTO Cards ( code_number ) VALUES ( LPAD( FLOOR( RAND() *
100000 ), 5, '0' ) )


From: ZeldorBlat Date:   Wednesday, October 24, 2007
wrote in message:
Hello all -
I have an SQL statement that generates a random code number. In the
table definition, the code number has a unique index, so my concern is
that the randomly-generated number could fail the UNIQUE constraint.
What PHP control structure can I use to keep re-trying a query until
it does not fail? Most of the time it'll successfully insert, and my
guess is that it'll only need a second try to generate an unused
number. But, I'd like to have bullet-proof code to keep trying the
statement until it does not fail.
How could I do this? Here is my sql statement:
INSERT INTO Cards ( code_number ) VALUES ( LPAD( FLOOR( RAND() *
100000 ), 5, '0' ) )



while(true) {
if(mysql_query('insert into cards...'))
break;
}

According to the manual, mysql_query() will return true on success and
false on error -- so just keep doing it until it returns true.

Of course there's no way to guarantee that the query will ever succeed
so you might want to write it so that it eventually gives up:

for($i = 0; $i < 1000; $i++) {
if(mysql_query('insert into cards...'))
break;
}

If it does not work after trying 1000 times it'll stop.



Next Message: [PHP] CURL + Frames, fopen + remote sessions


Blogs related to control structure for a falied MySQL query?

B2
mazda b2300 peser lossb2 t1.403 db25 b2k lil fizz chat libérer mobimb24 chargement sh: db2: not found mazda 4x2 b2600 hbb250s parts americhinab2b.com 174225-b21 b2300 b&d bread machine recipes mdb2 mysql dsn b29 bomber designs ...

Backup
... battery backup for sump pumps cracked full backup-burner backup på internet backup nb5 veritas backup exec illegal xml character backup c&c tiberium ntbackup and command scripts 'mminfo query and backup status' baxkup replacing 83 ...

Backup
''mminfo backup results' the browser service has failed to retrieve the backup list restore errors using netbackup 6 pontiac backup sensor 'how to backup returns in taxwise 2006' amt backup takedown guide vehicle backup cameras chrysler ...

Backup
... free download avl solar battery backup mythbackup mysql error step7 backup siemens cppbackup novabackup sump pump backup power systems pfbackup.pst restore folder.cfg backup-to-disk kfwebserver cgiserver net webhosting servers php ...

B2
... rebind universal auto sync cable 250177-b21+palm os db2 explzin http://touzoku.sakura.ne.jp/kiki/phpbb2/index.php audiovox remote control 136b2127 sirius sp-b2 d-link dwl-g120 version b2 dippach b24 monitor audio fb210 century motor ...

Backup
... backups route 78 mount dismount tape +ntbackup backup process failed: rman exited with return code '1' backup carosel suse backup mail kontact honda cr-v backup sensor installing backup exec on a sql 2005 computer backup mysql users ...


Programming | Sports | Autos

copyright 2006
Valid XHTML 1.0 Transitional