Welcome to WebmasterWorld Guest from 54.145.174.178

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Function returning only nulls

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

5+ Year Member



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; }
}
}
7:49 pm on Jun 4, 2013 (gmt 0)

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



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
8:13 pm on Jun 4, 2013 (gmt 0)

5+ Year Member



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

Featured Threads

Hot Threads This Week

Hot Threads This Month