I'm having a problem with the following PHP script. Specifically, the part that creates the user_id. This is part of a larger registration.php file that works fine without the section that creates the user_id.
As you can see, it's a while loop that uses a variable, $useridexits, to control the loop.  It's set to true by default, so the loop runs.  A random number is generated and then checked against the database.  If a result is returned, the $useridexists variable is set to true and the loop continues.  If no results are returned, $useridexists is set to false, so the loops stops.  The number generated is then set to $userid and is then added to the database in the following section.  
Here's the code:
//This section creates a new userid for each user.  
//This varible is used by the while loop and is set to true by default. 
$useridexists = true; 
//While loop to create userid and check the database to see if the userid
//already exists.  If it does, the while loop keeps going until a unique 
//user id is created.  
while($useridexists){
    // Function to create random user id number.
    function randomNumber($length) {
        $result = '';
        for($i = 0; $i < $length; $i++) {
            $result .= mt_rand(0, 9);
        }
        return $result;
    }
    // user id value created from randomNumber function. 
    $number = randomNumber(1);
    //query the database to see if the user id already exists 
    $query = "SELECT * FROM users WHERE user_id = :number";
    $query_params = array(':number' => '$number');
    try {
        // These two statements run the query against the database table. 
        $stmt   = $db->prepare($query);
        $result = $stmt->execute($query_params);
    }
    catch (PDOException $ex) {
        $response["success"] = 0;
        $response["message"] = "Failed to run query: " . $ex->getMessage();
        die(json_encode($response));
    }
    $row = $stmt->fetch();
    if ($row){
        $useridexists = true;
    }else{
        $useridexists = false; 
    }
}
$userid = $number; 
// This section adds the values to the database:
$query = "INSERT INTO users (username, password, email, firstname, lastname, user_id) VALUES ( :user, :pass, :email, :firstname, :lastname, :uid)";
//update tokens with the actual data:
$query_params = array(
    ':user' => $_POST['username'],
    ':pass' => $_POST['password'], 
    ':email' => $_POST['email'],
    ':firstname' => $_POST['firstName'], 
    ':lastname' => $_POST['lastName'],
    ':uid' => $userid
);
//run the query, and create the user
try {
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Failed to run query: " . $ex->getMessage();
    die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "Username Successfully Added! Please log in.";
echo json_encode($response);
$email= mysql_escape_string($_POST['email']);
$username = mysql_escape_string($_POST['username']); 
If I comment out this section, everything works:
// user id value created from randomNumber function. 
$number = randomNumber(1);
//query the database to see if the user id already exists 
$query = "SELECT * FROM users WHERE user_id = :number";
$query_params = array(
    ':number' => '$number'
    );
try {
    // These two statements run the query against the database table. 
    $stmt   = $db->prepare($query);
    $result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
    $response["success"] = 0;
    $response["message"] = "Failed to run query: " . $ex->getMessage();
    die(json_encode($response));
}
$row = $stmt->fetch();
if ($row){
    $useridexists = true;
}else{
    $useridexists = false; 
}
If I don't comment that section out, I don't get any errors, but nothing gets added to the database.
Everything works except the part that checks the database to see if the user_id already exists and changes the $useridexists variable to false, which should escape the while loop.  When I add that, nothing gets added to the database.  
BTW: I'm using a 1 digit value for testing purposes, but I'll change it to $number = randomNumber(7); once the code actually works. 
