views:

96

answers:

2

my code-

function create_id()
{  
    //global $myusername;  
    $part1 = substr("Piyush", 0, -4);  
    $part2 = rand (99,99999);  
    $part3 = date("s");  
    return $part1.$part2.$part3;  
}

echo create_id();  //this is printing fine.

function isUniqueUserID($userIDToCheck)    
{
    $sqlcheck = "Select * FROM ruser WHERE userId='$userIDToCheck';";    
    $resource = mysql_query($sqlcheck)or die(mysql_error());   
    $count = mysql_fetch_assoc($resource);  
    if( count($count) > 0)  
    {return false;}  

    return true;  
}


$userIDVerifiedUnique = false;  
while(! $userIDVerifiedUnique )  
{  
    $userIDToCheck = create_id();  
    $userIDVerifiedUnique = isUniqueUserID($userIDToCheck );  
}

loop is just going on and on from while loop to function IsUniqueUser() and vice versa.????

+1  A: 

First, try changing your isUniqueUserID() function to this

function isUniqueUserID($userIDToCheck)
{

$userIDToCheck = mysql_real_escape_string($userIDToCheck); //prevent SQL injection

$sqlcheck = "Select userId FROM ruser WHERE userId='$userIDToCheck';";
$resource = mysql_query($sqlcheck)or die(mysql_error());
$count = mysql_num_rows($resource);

return ($count > 0) ? false : true;

There's no point in returning an associative array just to count the number of rows in it. And there's no point in doing a SELECT * when counting just do SELECT userId since that's all you're concerned with.

I don't see any other reason that isUniqueUserID() would return false unless your ruser table has every possible ID.

jordanstephens
Or `"select count(*) FROM ruser WHERE userId='$userIDToCheck';";` and please fix the sql injection risk if `$userIDToCheck` comes from user input
Dinah
`select count(*)` would also work, but i would still do `select count(userId)` to prevent from selecting everything in the row. and i added a line to prevent sql injection
jordanstephens
`count(*)` is idiomatic SQL and shouldn't ever need to read the full row unless there is something terribly wrong with your DBMS. It should be the preferred form.
zinglon
thanks man....that count was creating problem.
nectar
@zinglon Sorry I wasn't aware of that. Thanks!
jordanstephens
+2  A: 

If there are no rows returned from the MySQL query (i.e. the $userIDToCheck is not in the table, it is unique) then mysql_fetch_assoc will return FALSE. When that happens, count(FALSE) returns 1 (one)! Since that value is greater than zero the function returns FALSE.

In short, if there is a row returned (the string is not unique) your isUniqueUserID function returns FALSE; if there is no row returned (the string is unique) it still returns FALSE.


A simple, new, function to check on the database table could look something like the following...

function isUniqueUserID($userIDToCheck)
{
    $userIDToCheck = mysql_real_escape_string($userIDToCheck); // Assume not already escaped
    $sqlcheck = "SELECT 1 FROM ruser WHERE userId='$userIDToCheck' LIMIT 1";    
    $resource = mysql_query($sqlcheck) or die(mysql_error());
    return (bool) mysql_num_rows($resource);  
}
salathe
thats what I know then what changes I have to make??that was my Q.
nectar
@Piyush, see the function in my answer... though I notice you've accepted the other one. P.S. Your Q didn't ask what changes need to be made.
salathe