Welcome! Log In Create A New Profile

Advanced

Function not returning expected value

Posted by Schalk1807 
Announcements Last Post
Announcement myUnisa availability 21 to 24 March 2019 03/17/2019 02:24PM
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Function not returning expected value
October 29, 2012 09:44AM
Good morning,

My portfolio is finished, however for the last 3 days I have been struggling with this one last problem. Even 2 x php help phorums could not assist.

Can anyone tell me why this function is returning the user_id instead of first_name and last_name? Query does not fail.

Language: PHP
function getFriendsById($friends) { $sql="select first_name, last_name from users where user_id =".$friends; $results = mysql_query($sql)or die("Selection Query Failed !!!<br/>".mysql_error()); $friendsNames = array(); while ($row = mysql_fetch_array($results)){ $friendsNames[] = $row['first_name'].', '.$row['last_name']; } return $friendsNames;   }
avatar
Mac
Re: Function not returning expected value
October 29, 2012 11:07AM
Map the value to their keys by using mysql_fetch_assoc

Language: PHP
$friendsNames = array(); while($row = mysql_fetch_assoc($result)) { $friendsNames [] = $row; }
Re: Function not returning expected value
October 29, 2012 11:49AM
Done as you said.

Same result. confused smiley

Language: PHP
function getFriendsById($friends) { $sql="select first_name, last_name from users where user_id =".$friends; $results = mysql_query($sql)or die("Selection Query Failed !!!<br/>".mysql_error()); $friendsNames = array(); while ($row = mysql_fetch_assoc($results)){   } return $friendsNames [] = $row;   }
avatar
Mac
Re: Function not returning expected value
October 29, 2012 02:17PM
I have no idea what you are trying to achieve. It appears as if you are trying to find the friends of a user, but the user table does not have that information?
As the code stands, you are calling one record/row (as specified by the WHERE clause), so you do not need an array?
I suggest you write the query outside a function at first, and only put it in a function once you will re-use it.
Re: Function not returning expected value
October 29, 2012 02:28PM
I am sending the friends_id's to the getFriendsById function with the $friends variable.
Then I am requesting for the friends first and last names from the user table using the friends_id's.

This all happenes. The variable does indeed key the function with the correct friends_id's, and the query does not fail.

However instead of returning the friends first and last names, the function simply returns the friends_id's again.
avatar
Mac
Re: Function not returning expected value
October 29, 2012 03:22PM
Mmm. I do not know what you are doing before employing this function.

You get all the friends ID's from user_friends table. That should go into an array, and while looping through this array you use a query to extract the first and last name. It seems to me you are doing it the other way round.....
Re: Function not returning expected value
October 29, 2012 04:09PM
Yes, I obtain the friends id's, using the function you provided in tut 102 and store those values in the $friends array. (lets say id's are 2 and 5.)

( Start of code snippet above) I then use the $friends array to key the getFriendsById function.(With value 2 and 5) --> successful.

Now I attampt to extract the friends first and last names using the friends id's (2 , 5) --> successful, query does not fail.

Then I place the values into the friendsNames array.

Then the function returns 2 and 5, instead of Elvis Presley, Lucas Radebe)

I'm completely snookered. The last 3 day I've spent on php help phorems and was assisted by "expert php masters", but no one can see or find a problem.
I even thought the problem might be my localhost, but I uploaded all to web server and outcome is the same.
avatar
Mac
Re: Function not returning expected value
October 30, 2012 09:28AM
I'm stumped as well.

Just try the following as a test to see if the names are printed, else look at your db.

Language: PHP
function getFriendsById($friends) { $sql="select first_name, last_name from users where user_id =".$friends; $results = mysql_query($sql)or die("Selection Query Failed !!!<br/>".mysql_error()); //$friendsNames = array(); while ($row = mysql_fetch_array($results)){ echo "<br>Friends with ". $row['first_name']. " ".$row['last_name']. "<br>"; } //return $friendsNames; }
Re: Function not returning expected value
October 31, 2012 11:54AM
mac,

You are going to swear at me. I never called the getFriendsById function. I recalled the getFriends function, and that is why it kept on returning the friends id's. eye popping smiley

Unfortunately I only found the mistake now and all ready had to submit. grinning smiley Hope you will still pass me.

My apologies for waisting your time.
avatar
Mac
Re: Function not returning expected value
October 31, 2012 12:30PM
Ha, ... it usually is an error elsewhere, but difficult to pinpoint it because there are so many places where it can happen. I think you have learned a valuable lesson though...
Re: Function not returning expected value
October 31, 2012 12:36PM
Believe me, a lesson I will not easily forget.

Again, thanx for all your assistance and advice.
Sorry, only registered users may post in this forum.

Click here to login