I’m trying to figure out why it returns 1 as the integer when I print the max_user_id the very first time I run this function which exists inside my library. It should be returning 10000. I have also included the users model function as well. Do want to make a point that at the point the model function is ran the first time it is empty.
Library
/**
* Create new user on the site and return some data about it:
* user_id, username, password, email, new_email_key (if any).
*
* @param string
* @param string
* @param string
* @param string
* @param string
* @return array
*/
function create_user($username, $email, $password, $first_name, $last_name)
{
if ((strlen($username) > 0) AND !$this->ci->users->is_username_available($username))
{
return FALSE;
}
elseif (!$this->ci->users->is_email_available($email))
{
return FALSE;
}
else
{
$genPassHash = $this->ci->genfunc->GenPassHash($password);
$max_user_id = $this->ci->users->get_max_users_id();
echo $max_user_id;
$user_data = array(
'username' => $username,
'password' => $genPassHash[0],
'password2' => $genPassHash[1],
'email' => $email,
'first_name' => $first_name,
'last_name' => $last_name,
'new_email_key' => md5(rand().microtime()),
'user_id' => $max_user_id,
'ip_address' => $this->ci->input->ip_address()
);
if ($this->ci->users->create_user($user_data) == FALSE)
{
$user_data['user_id'] = $res['user_id'];
$user_data['password'] = $password;
unset($user_data['last_ip']);
return $user_data;
}
}
return FALSE;
}
Model:
/**
* Get the max int of users id
*
* @return int
*/
function get_max_users_id()
{
$this->db->select_max('user_id');
$query = $this->db->get('users');
if ($query->num_rows() > 0)
{
$row = $query->row();
return $row->user_id +1;
}
else
{
return 10000;
}
}
Always returns 1 row. If no rows exist, it returns a row with NULL as a value.
So, you’ll need to do:
Please tell me that you’re not using this to do insertions though. What if two users go to get created at the exact same time, and both try to create with ID 7? Auto incrementing fields exist for a reason.