[eluser]stevefink[/eluser]
Hi all,
This isn't a CI specific issue, more like a user error on my behalf. ;-) I'm almost embarrassed to ask for a second pair of eyes on something this rudimentary, however I seem to keep running into an inconvenient complication.
I'm essentially looking to assign a random 7 digit number to a column in a mysql table which is declared as the following:
`anonymous_extension` mediumint(9) default NULL,
My code looks something like this:
Code:
do {
$num = (int) rand(1000000, 9999999);
} while ($this->dbmodel->check_num($num));
and the check_num function:
Code:
function check_num($n)
{
$sql = "SELECT num
FROM foo
WHERE num = ?";
$query = $this->db->query($sql, array($e));
if($query->num_rows() > 0) {
log_message('debug', "We have a problem, houston. We need another num! ");
return TRUE;
} else {
$row = $query->row();
log_message('debug', "This num should be good to go, returning false! . {$row->anonymous_extension}");
return FALSE;
}
}
A lot of the time I get the same number as per the following:
DEBUG - 2008-04-23 23:30:54 --> This num should be good to go, returning false!
ERROR - 2008-04-23 23:30:54 --> Query error: Duplicate entry '8388607' for key 2
This in turn also breaks my MySQL transactions and I need to restart MySQL which really hurts my application, but that's in another thread... the question here is, where is my logic flawed that I keep getting duplicate numbers? Am I doing something that's blatantly obvious that anyone can spot?
Thanks for the advice...
/sf