homepage Welcome to WebmasterWorld Guest from 54.166.228.100
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Function returning only nulls
webfoo




msg:4581113
 7:38 pm on Jun 4, 2013 (gmt 0)

Hello all, it's been a while since I've been on WebmasterWorld. I'm having trouble with a function which will be used frequently throughout the system I'm developing. It is supposed to take in a staff ID number, and returns the staff member's name.

$con is defined previously in the file and works for all other queries, so that's not the issue. The function is not returning any mysqli_errors, so that's not the issue either. Also, the query $q works when entered directly into MySQL. Therefore, the problem lies somewhere in the PHP logic.

I'm only getting null values out of this function. What am I doing wrong?!?!

function nameTheStaff($x) {
$q="SELECT * FROM staff WHERE staff_id='$x'";
$result=mysqli_query($con,$q);
while($r=mysqli_fetch_array($result)) {
$staff=$r['first_name'].' '.$r['last_name'];
}
if (mysqli_errno()) { return mysqli_error(); }
else { return $staff; }
}
}

 

brotherhood of LAN




msg:4581118
 7:49 pm on Jun 4, 2013 (gmt 0)

It's returning null because there isn't a database connection being used.

$con is not in the function's variable scope. You need to either make it global, or pass the $con variable along to the function.

See variable scope [php.net] for reference

webfoo




msg:4581125
 8:13 pm on Jun 4, 2013 (gmt 0)

Duh! Thank you for pointing out the obvious mistake. Working properly now.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved