Page 1 of 1

i have a little issue with this function

Posted: Thu Jan 24, 2008 11:28 pm
by scheinarts
i made this function which is supposed to get the person's name from the database and make a username

Code: Select all

$getUserNameQuery_string = "SELECT * FROM phil_clientes WHERE phil_clientes.id = '{$thisRecord}'";
$getUserNameQuery = mysql_query($getUserNameQuery_string) or die(mysql_error());
 
function makeUserName(){
    while($row = mysql_fetch_assoc($getUserNameQuery)){
        $cn = $row['name'];
        $cn = strtolower($cn);
        if(strpos(" ") !== FALSE){// space
            $s = explode(" ", $cn);
            $d = $s[0];
        }else if(strpos(",") !== FALSE){//comma
            $s = explode(",", $cn);
            $d = $s[0];
        }else if(strpos(".") !== FALSE){//dot
            $s = explode(".", $cn);
            $d = $s[0];
        }else{
            $d = $cn;
        }
    }
    echo $d;
    return $d;
}
but its not returning anything, blank, zip, none and there are also no errors

Can someone please tell me what I did wrong ? And yes, $thisRecord does contain a value

Re: i have a little issue with this function

Posted: Thu Jan 24, 2008 11:36 pm
by Christopher
How about:

Code: Select all

function makeUserName($thisRecord){
    $getUserNameQuery_string = "SELECT * FROM phil_clientes WHERE phil_clientes.id = '{$thisRecord}'";
    $getUserNameQuery = mysql_query($getUserNameQuery_string) or die(mysql_error());
 
    $row = mysql_fetch_assoc($getUserNameQuery);
        $cn = $row['name'];
        $cn = strtolower($cn);
        if(strpos(" ") !== FALSE){// space
            $s = explode(" ", $cn);
            $d = $s[0];
        }else if(strpos(",") !== FALSE){//comma
            $s = explode(",", $cn);
            $d = $s[0];
        }else if(strpos(".") !== FALSE){//dot
            $s = explode(".", $cn);
            $d = $s[0];
        }else{
            $d = $cn;
        }
 
    echo $d;
    return $d;
}

Re: i have a little issue with this function

Posted: Thu Jan 24, 2008 11:46 pm
by scheinarts
WOW! that worked!!! Why is then that the variable $thisRecord wasnt being recognized inside the function if it was already declared outside the function (and before the function is written) and had value too? This way works fine in javascript, why not in php?

Re: i have a little issue with this function

Posted: Fri Jan 25, 2008 8:01 am
by mattcooper
This is due to a scope issue - your resultset is outside of the function and therefore not visible to it - you have to run the query inside the function. Another way to do this might have been to use mysql_fetch_object ouside of the function and then pass the object into the function as an argument. That way, your data would be accessible via (untested):

Code: Select all

 
$getUserNameQuery_string = "SELECT * FROM phil_clientes WHERE phil_clientes.id = '{$thisRecord}'";
$getUserNameQuery = mysql_query($getUserNameQuery_string) or die(mysql_error());
while ($res = mysq_lfetch_object($getUserNameQuery)) $object = $res;
 
function makeUserName($object)
{
    $name = explode(" ", $object->name);
    return $name[0];
}
 
echo makeUserName($object);
 
Using objects is a nicer way of doing things IMHO. HTH.

Re: i have a little issue with this function

Posted: Fri Jan 25, 2008 9:11 am
by Ollie Saunders
JavaScript and PHP have different scope behaviour. There's a section in the PHP manual on scope rules.