i have a little issue with this function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

i have a little issue with this function

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: i have a little issue with this function

Post 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;
}
(#10850)
scheinarts
Forum Commoner
Posts: 52
Joined: Wed Jul 25, 2007 2:37 am

Re: i have a little issue with this function

Post 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?
User avatar
mattcooper
Forum Contributor
Posts: 210
Joined: Thu Mar 17, 2005 5:51 am
Location: London, UK

Re: i have a little issue with this function

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Re: i have a little issue with this function

Post by Ollie Saunders »

JavaScript and PHP have different scope behaviour. There's a section in the PHP manual on scope rules.
Post Reply