Function not returning expected value

Can anyone please tell me why this function is returning the user_id as contained in the $friends variable instead of the first_name and last_name?

[php]
public function getFriendsById($friends) {
$sql=“SELECT first_name, last_name FROM users WHERE user_id =”. $friends;
$results = mysql_query($sql)or die(“Selection Query Failed !!!”);
$friendsNames = array();
while ($row = mysql_fetch_array($results)){
$friendsNames[] = $row[‘first_name, last_name’];
}
return $friendsNames;
}

}	

[/php]

try
[php]
public function getFriendsById($friends) {
$sql=“SELECT first_name, last_name FROM users WHERE user_id =’”.$friends."’";
$results = mysql_query($sql)or die(“Selection Query Failed !!!
”.mysql_error());
$friendsNames = array();
while ($row = mysql_fetch_array($results)){
$friendsNames[] = $row[‘first_name’].’, '.$row[‘last_name’];
}
return $friendsNames;
}
[/php]

Thanx for assist.

Query did not fail.
Result still the same. :frowning:

Anything else I could try?

i will test in my localhost

thank you

[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 !!!
”.mysql_error());
$friendsNames = array();
while ($row = mysql_fetch_array($results)){
$friendsNames[] = $row[‘first_name’].’, '.$row[‘last_name’];
}
return $friendsNames[0];
}
[/php]

the result should be firstname, lastname

good luck

Thank you for taking all this time to help me!!

Unfortunately it didn’t work.

???

Unable to + your karma, maybe disabled for neewbs?

you need 25 post in order to karma me.

can you be more specific when you say it didnt work?

what does it do? what does it return?

maybe you are merging this function with other codes that might have errors as well.

i tested this function and it worked for me

It still returns the user_id

Calling and returning to:

[php]
if (isset($_REQUEST[“submit2”]))
{

$ID=$_REQUEST["user2"] + 1;
$user = new User();
$user->getUserById($ID);

echo "<p>User known as: " .$user->getDisplayName()."</p>";

/*calling getFriends() function and assigning that fallue to $friends variable*/

$friends = $user->getFriends();
foreach ($friends as $friendsNames){

	
echo "<p>Friends with: ". $friendsNames ."<p>";

}

}
[/php]

show me all the involved code

Apologies for only replying now. When you replied it was after 23:00 and I had to get up for work at 05:00.

Here is all the code.

First the form. 2 x seperate forms on seperate pages:
[php]

<?php /*connecting oop4.php to user_find.php using the include keyword*/ include ("user_find.php"); ?>
<form name='users' method='POST' action='oop4.php'>
Peter Pan Elvis Presley Pollen Ndlanya Clark Kent Kaizer Motaung Lucas Radebe [/php]

Second Form:

[php]

<?php include ("user_find.php"); ?>
<form name='users' method='POST' action='oop5.php'>
Peter Pan Elvis Presley Pollen Ndlanya Clark Kent Kaizer Motaung Lucas Radebe

[/php]

Next the user_find page.

[php]

<?php /*connecting user_find.php with user.php using connect keyword*/ include ("user.php"); /*connecting to database and tables using mysql commands.*/ mysql_connect("127.0.0.1", "root", "")or die ("Could not connect to server"); mysql_select_db("490942")or die ("Could not connect to database"); /*Check if submit button in oop4.php was pressed and if so, assigning value to $ID. Calling getUserById function and sending $ID to that function as argument.*/ if (isset($_REQUEST["submit1"])) /*Assigning value to $ID + 1 because option values start at 0, but user id's in user table auto increment from 1.*/ { $ID=$_REQUEST["user1"] + 1; $user = new User(); $user->getUserById($ID); /*calling getFirstName and getLastName function to return name of person whose user ID was sent to getUserById function*/ echo "

User's name is: " .$user->getFirstName(). " " .$user->getLastName()."

"; /*Calling getFreinds function and assigning return value to $friens. The .implode keyword is used to join array elements with a string, as $friens is an array.*/ $friends = $user->getFriends(); echo "

Friends with: " .implode(', ',$friends). "

"; echo "

User known as: " .$user->getDisplayName(). "

"; } /*Check if submit button in oop5.php was pressed and if so, assigning value to $ID. Calling getUserById function and sending $ID to that function as argument.*/ if (isset($_REQUEST["submit2"])) { $ID=$_REQUEST["user2"] + 1; $user = new User(); $user->getUserById($ID); echo "

User known as: " .$user->getDisplayName()."

"; /*calling getFriends() function and assigning that fallue to $friends variable*/ $friends = $user->getFriends(); foreach ($friends as $friendsNames){ echo "

Friends with: ". $friendsNames ."

"; } } ?>

[/php]

Lastly the user page

[php]

<?php /*Creating the class for the functions to be used by the code in user_find.php*/ class User{ /*Function used to capture the user_id and to extract the details from the database table "users" and using the $this keyword to point to the current objects*/ public function getUserById($user_id){ $sql="SELECT 'first_name', 'last_name', 'display_name' FROM 'users' WHERE 'user_id' ='". $user_id"'"; $results = mysql_query($sql); $row = mysql_fetch_array($results); $this->user_id = $user_id; $this->first_name = $row['first_name']; $this->last_name = $row['last_name']; $this->display_name = $row['display_name']; return true; } /*function called to return display_name to user_find.php using the $this keyword*/ public function getDisplayName (){ return $this->display_name; } /*function called to return first_name to user_find.php using the $this keyword*/ public function getFirstName (){ return $this->first_name; } /*function called to return last_name to user_find.php using the $this keyword*/ public function getLastName (){ return $this->last_name; } /*function called to extract the friends id's using a sql query and returning value in the friends array variable*/ function getFriends(){ $sql="select friend_id from user_friends where user_id =" .$this->user_id; $results = mysql_query($sql); $friends = array(); while ($row = mysql_fetch_array($results)){ $friends[] = $row['friend_id']; } return $friends; } /*function called to extract friends first and last names using sql query and returning value to user_find.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 !!!
".mysql_error()); $friendsNames = array(); while ($row = mysql_fetch_array($results)){ $friendsNames[] = $row['first_name'].', '.$row['last_name']; } return $friendsNames[0]; } } ?>

[/php]

i fixed some errors

form1
[php]

<?php /*connecting oop4.php to user_find.php using the include keyword*/ include ("user_find.php"); ?> Peter Pan Elvis Presley Pollen Ndlanya Clark Kent Kaizer Motaung Lucas Radebe [/php]

form2
[php]

<?php include ("user_find.php"); ?>
<form name='users' method='POST' action='oop5.php'>
Peter Pan Elvis Presley Pollen Ndlanya Clark Kent Kaizer Motaung Lucas Radebe

[/php]

user_find
[php]

<?php /*connecting user_find.php with user.php using connect keyword*/ include ("user.php"); /*connecting to database and tables using mysql commands.*/ mysql_connect("127.0.0.1", "root", "")or die("Could not connect to server"); mysql_select_db("490942")or die("Could not connect to database"); /*Check if submit button in oop4.php was pressed and if so, assigning value to $ID. Calling getUserById function and sending $ID to that function as argument.*/ if (isset($_REQUEST["submit1"])) /*Assigning value to $ID + 1 because option values start at 0, but user id's in user table auto increment from 1.*/ { $ID=$_REQUEST["user1"] + 1; $user = new User(); $user->getUserById($ID); /*calling getFirstName and getLastName function to return name of person whose user ID was sent to getUserById function*/ echo "

User's name is: " .$user->getFirstName(). " " .$user->getLastName()."

"; /*Calling getFreinds function and assigning return value to $friens. The .implode keyword is used to join array elements with a string, as $friens is an array.*/ $friends = $user->getFriends(); echo "

Friends with: " .implode(', ',$friends). "

"; echo "

User known as: " .$user->getDisplayName(). "

"; } /*Check if submit button in oop5.php was pressed and if so, assigning value to $ID. Calling getUserById function and sending $ID to that function as argument.*/ if (isset($_REQUEST["submit2"])) { $ID=$_REQUEST["user2"] + 1; $user = new User(); $user->getUserById($ID); echo "

User known as: " .$user->getDisplayName()."

"; /*calling getFriends() function and assigning that fallue to $friends variable*/ $friends = $user->getFriends(); foreach ($friends as $friendsNames){ echo "

Friends with: ". $friendsNames ."

"; } } ?>

[/php]

users
[php]

<?php /*Creating the class for the functions to be used by the code in user_find.php*/ class User{ /*Function used to capture the user_id and to extract the details from the database table "users" and using the $this keyword to point to the current objects*/ public function getUserById($user_id){ $sql="SELECT `first_name`, `last_name`, `display_name` FROM `users` WHERE `id` ='".$user_id."'"; $results = mysql_query($sql) or die(mysql_error()); $row = mysql_fetch_array($results); $this->user_id = $user_id; $this->first_name = $row['first_name']; $this->last_name = $row['last_name']; $this->display_name = $row['display_name']; return true; } /*function called to return display_name to user_find.php using the $this keyword*/ public function getDisplayName (){ return $this->display_name; } /*function called to return first_name to user_find.php using the $this keyword*/ public function getFirstName (){ return $this->first_name; } /*function called to return last_name to user_find.php using the $this keyword*/ public function getLastName (){ return $this->last_name; } /*function called to extract the friends id's using a sql query and returning value in the friends array variable*/ function getFriends(){ $sql="select friend_id from user_friends where user_id =" .$this->user_id; $results = mysql_query($sql) or die(mysql_error()); $friends = array(); while ($row = mysql_fetch_array($results)){ $friends[] = $row['friend_id']; } return $friends; } /*function called to extract friends first and last names using sql query and returning value to user_find.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 !!!
".mysql_error()); $friendsNames = array(); while ($row = mysql_fetch_array($results)){ $friendsNames[] = $row['first_name'].', '.$row['last_name']; } return $friendsNames[0]; } } ?>

[/php]

if it still not working i will help thru teamviewer becuase i might not fully understand what you want to acomplish

I don’t see where you are ever calling the getFriendsById() function? It looks like the getFriends() function returns the IDs and the getFriendsById() returns the first/last names.

I can see you calling getFriends() here:

[php]
$friends = $user->getFriends(); // should’t this be getFriendsById() ??
foreach ($friends as $friendsNames) {
echo “

Friends with: “. $friendsNames .”

”;
}
[/php]

[size=30pt]Matt is right.[/size]

Wilson382

Can you believe it!! I am quite new to the coding community but as you both probably allready know, it is always te smallest most infidecimal mistakes that keeps you buggered for the longest times, like a missing ; or { or spelling mistake.

Thank you so so much for your time and effort. As I am new to coding forums as well, it amazes me that people like yourself and m@tt takes time out of your own lives to assist people you don’t even know.

All the best to you and your family.

m@tt,

You are absolutely right. I never called the function. I now get all new kinds of warnings and notices but at least I can move forward.

As I told Wilson382, I am truly amazed and thankful.

All the best to you and your family 2.

Sponsor our Newsletter | Privacy Policy | Terms of Service