Jump to content

PHP MYSQL get a users first name and surname from their user ID


dannyp100

Recommended Posts

I'm currently writing a function that will return a users firstname and surname instead of their user ID.

I need help with the code and logic.

 

Here is my function:

 

<?php
function getNameFromID($userID){

include 'gradnetconn.php';
$query = ("SELECT userFirstName , userSurname FROM gn_users WHERE userID = userID");
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
return $row['userFirstName\'. \'userSurname'];
}
}

 

Here is where i return it in a users inbox to see who their message was from:

 

<?php
include 'gradnetconn.php';
include 'functions.php';
session_start();

$messageTo = $_SESSION['userID'];
$sql = mysql_query("SELECT * FROM gn_messages WHERE messageTo = $messageTo AND messageDeleted = '0'
ORDER BY messageDate DESC");
?>
<table width='95%'>
<tr><th>From</th><th>Message Subject</th><th>Date Recieved</th></tr>
<?php
 while($return = mysql_fetch_object($sql)) {

  $messageDate = gmdate("M d Y H:i:s",$return->messageDate);
  if($return->messageRead == "0") {
  $messageRead = "notread.jpg";
  }
  else {
  $messageRead = "read.jpg";
  }
  echo "<tr><td><img src='".$messageRead."' /></td>

$fromUser = getNameFromID($return->messageFrom);

<tr><td>$return->fromUser</td><td><a href='viewmessage1.php?messageID=$return->messageID'>$return->messageSubject</a>
  </td><td>$messageDate</td></tr>";
}

?>
</table>

 

 

Any help/Suggestions?

 

Thanks

Link to comment
Share on other sites

You can't just concatenate random stuff inside an array key string and expect it to do anything.

 

You can however concat it IN the query. Either of these.

SELECT CONCAT_WS(' ', userFirstName, userSurname) AS full_name FROM gn_users -- Rest of query
SELECT CONCAT(userFirstName, ' ', userSurname) AS full_name FROM gn_users -- Rest of query

Link to comment
Share on other sites

Tried to SQL statement and it works perfectly, thankyou so much!

All i need to understand now is how to put it in a row and then return it on the inbox page so it displays full_name?

 

<?php
function getNameFromID($userID){

include 'gradnetconn.php';
$query = ("SELECT CONCAT(userFirstName, ' ', userSurname) AS full_name FROM gn_users WHERE userID = userID");
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
return $row['full_name'];
}
}

 

Then the part on the bottom of my inbox code:

 

$fromUser = getNameFromID($return->messageFrom);
  <tr><td>$fromUser</td><td><a href='viewmessage1.php?messageID=$return->messageID'>$return->messageSubject</a>
  </td><td>$messageDate</td></tr>";
}

?>
</table>

 

Comes up with errors

Notice: Undefined variable: fromUser on line 34

 

Notice: Undefined variable: fromUser on line 36

= getNameFromID(6);

 

thanks

Link to comment
Share on other sites

functions.php

 

<?php
function getNameFromID($userID){

include 'gradnetconn.php';
$query = ("SELECT CONCAT(userFirstName, ' ', userSurname) AS full_name FROM gn_users WHERE userID = userID");
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo( $row['full_name']);
}
}

 

inbox code:

 

<?php
include 'gradnetconn.php';
include 'functions.php';
session_start();

$messageTo = $_SESSION['userID'];
$sql = mysql_query("SELECT * FROM gn_messages WHERE messageTo = $messageTo AND messageDeleted = '0'
ORDER BY messageDate DESC");
?>
<table width='95%'>
<tr><th>From</th><th>Message Subject</th><th>Date Recieved</th></tr>
<?php
 while($return = mysql_fetch_object($sql)) {

  $messageDate = gmdate("M d Y H:i:s",$return->messageDate);
  if($return->messageRead == "0") {
  $messageRead = "notread.jpg";
  }
  else {
  $messageRead = "read.jpg";
  }
  echo "<tr><td><img src='".$messageRead."' /></td>
  $fromUser = getNameFromID($return->messageFrom);
  <tr><td>$fromUser</td><td><a href='viewmessage1.php?messageID=$return->messageID'>$return->messageSubject</a>
  </td><td>$messageDate</td></tr>";
}

?>
</table>

Link to comment
Share on other sites

apologies!! I have just realised a silly error, heres the ammended inbox code:

 

<?php
include 'gradnetconn.php';
include 'functions.php';
session_start();

$messageTo = $_SESSION['userID'];
$fromUser = getNameFromID($return->messageFrom);
$sql = mysql_query("SELECT * FROM gn_messages WHERE messageTo = $messageTo AND messageDeleted = '0'
ORDER BY messageDate DESC");
?>
<table width='95%'>
<tr><th>From</th><th>Message Subject</th><th>Date Recieved</th></tr>
<?php
 while($return = mysql_fetch_object($sql)) {

  $messageDate = gmdate("M d Y H:i:s",$return->messageDate);
  if($return->messageRead == "0") {
  $messageRead = "notread.jpg";
  }
  else {
  $messageRead = "read.jpg";
  }
  echo "<tr><td><img src='".$messageRead."' /></td>

  <tr><td>$fromUser</td><td><a href='viewmessage1.php?messageID=$return->messageID'>$return->messageSubject</a>
  </td><td>$messageDate</td></tr>";
}

?>
</table>

Link to comment
Share on other sites

Well for one, your function echos the text, not returns it. 

 

You also have random PHP inside a string. You can put variables inside a string, you can't put entire exp<b></b>ressions and functions inside them and have it work.

 

Edit: If you keep changing it we can't help you. Now your code is using $return before it's defined. Read your error messages and attempt them.

 

 

The HUGE problem is that you're running a query inside a loop by doing this. You should INNER JOIN the gn_messages table to the gn_users table and do ONE query.

Edited by Jessica
Link to comment
Share on other sites

Sorry if got confused, i fixed all the silly errors i made.

Heres the functions.php code:

 

<?php
function getNameFromID($userID){

include 'gradnetconn.php';
$query = ("SELECT CONCAT(userFirstName, ' ', userSurname) AS full_name FROM gn_users WHERE userID = userID");
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
return( $row['full_name']);
}
}

 

Inbox code:

 

<?php
include 'gradnetconn.php';
include 'functions.php';
session_start();

$messageTo = $_SESSION['userID'];

$sql = mysql_query("SELECT * FROM gn_messages WHERE messageTo = $messageTo AND messageDeleted = '0'
ORDER BY messageDate DESC");
?>
<table width='95%'>
<tr><th>From</th><th>Message Subject</th><th>Date Recieved</th></tr>
<?php
 while($return = mysql_fetch_object($sql)) {
  $fromUser = getNameFromID($return->messageFrom);
  $messageDate = gmdate("M d Y H:i:s",$return->messageDate);
  if($return->messageRead == "0") {
  $messageRead = "notread.jpg";
  }
  else {
  $messageRead = "read.jpg";
  }
  echo "<tr><td><img src='".$messageRead."' /></td>

  <tr><td>$fromUser</td><td><a href='viewmessage1.php?messageID=$return->messageID'>$return->messageSubject</a>
  </td><td>$messageDate</td></tr>";
}

?>
</table>

 

So it works now!

The only problem is, in the user ID field, it is returning the wrong name for the messages. It is returning

the name of 'Nial McGowan' which is the first full_name field when the query is run. I doesn't seem to be matching the Full_name with the correct userID

Link to comment
Share on other sites

$query = ("SELECT CONCAT(userFirstName, ' ', userSurname) AS full_name FROM gn_users WHERE userID = userID");

 

userID will always be equal to userID

 

 

Come on. 

 

You need to make this one query with an INNER JOIN. See my post above.

Link to comment
Share on other sites

I have never done inner joins before, i'm a total beginner.

I know this will be wrong but

$query = ("SELECT CONCAT(userFirstName, ' ', userSurname) AS full_name FROM gn_users INNER JOIN gn_messages WHERE userID = userID");

 

would the userID have to be equal to the 'messageTo' field is thats the column where the name appears in the inbox page?

Link to comment
Share on other sites

userID is a column.

userID is a column.

Does userID = userID? Yes, on EVERY SINGLE ROW. 

 

Does 1=1? Yes, on EVERY SINGLE ROW.

Does 'bob' = 'bob'? Yes, on EVERY SINGLE ROW.

 

See a theme?

 

 

You did it correctly for the first query:

"SELECT * FROM gn_messages WHERE messageTo = $messageTo AND messageDeleted = '0'
ORDER BY messageDate DESC"

 

Now add your inner join to that query. 

"SELECT gn_messages.*,  
CONCAT(userFirstName, ' ', userSurname) AS full_name
FROM gn_messages 
INNER JOIN gn_users
ON gn_users.user_id = gn_messages.messageTo -- assuming you want the user's name who it is to. You'll need to do this join AGAIN to get the sender's name as well.
WHERE messageTo = $messageTo AND messageDeleted = '0'
ORDER BY messageDate DESC"

 

On a totally unrelated note I suggest picking a naming convention and sticking with it. Switching from camelCase to underscores_and_back is really annoying.

Link to comment
Share on other sites

This is probably a rookie way of doing it, but these are the functions in order for the full name to display on the inbox and outbox

1st function for inbox, second for outbox

<?php
function getNameFromID1($userID){
include 'gradnetconn.php';
$query = ("SELECT CONCAT(userFirstName, ' ', userSurname) AS full_name FROM gn_users INNER JOIN gn_messages WHERE userID = messageFrom");
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
return( $row['full_name']);
}
}
?>
<?php
function getNameFromID2($userID){
include 'gradnetconn.php';
$query = ("SELECT CONCAT(userFirstName, ' ', userSurname) AS full_name FROM gn_users INNER JOIN gn_messages WHERE userID = messageTo");
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
 return( $row['full_name']);
}
}
?>

Link to comment
Share on other sites

Maybe I am not following this thread correctly but...

 

<?php
function getNameFromID1($userID){
include 'gradnetconn.php';
$query = ("SELECT CONCAT(userFirstName, ' ', userSurname) AS full_name FROM gn_users INNER JOIN gn_messages WHERE userID = messageFrom");
$result = mysql_query($query);
while($row = mysql_fetch_array($result)) {
return( $row['full_name']);
}
}
?>

 

this function has a variable $userID but you don't even use it in the query. You may wanna start with that.

 

 

EDIT: Also the JOIN does not have an ON clause.

Edited by DaveyK
Link to comment
Share on other sites

Still having trouble with the query. This is the code ive got now so on a users inbox, they can see the full_name of

the person that the message was from.

 

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource

 

 

 

<?php
include 'gradnetconn.php';
include 'functions.php';


session_start();



$messageFrom = $_SESSION['userID'];


$sql = mysql_query("SELECT gn_messages.*,  
CONCAT(userFirstName, ' ', userSurname) AS full_name FROM gn_messages INNER JOIN gn_users ON gn_users.user_id = gn_messages.messageFrom
WHERE messageFrom = $messageFrom AND messageDeleted = '0'");


?>


<table width='95%'>
<tr><th>From</th><th>Message Subject</th><th>Date Recieved</th></tr>


<?php


 while($return = mysql_fetch_object($sql)) {


$messageDate = gmdate("M d Y H:i:s",$return->messageDate);


  if($return->messageRead == "0") {
  $messageRead = "notread.jpg";
  }
  else {
  $messageRead = "read.jpg";
  }


  echo "<tr><td><img src='".$messageRead."' /></td>




  <tr><td>$return->messageFrom</td><td><a href='viewmessage1.php?messageID=$return->messageID'>$return->messageSubject</a>
  </td><td>$messageDate</td></tr>";
}



?>
</table>

Link to comment
Share on other sites

Would you be able to tell me why the correct user that the message is from isn't appearing.

This is my inbox code:

 

 

<?php
include 'gradnetconn.php';
include 'functions.php';


session_start();



$messageTo = $_SESSION['userID'];


$sql = mysql_query("SELECT * FROM gn_messages WHERE messageTo = $messageTo AND messageDeleted = '0'
ORDER BY messageDate DESC");


?>


<table width='95%'>
<tr><th>From</th><th>Message Subject</th><th>Date Recieved</th></tr>


<?php


 while($return = mysql_fetch_object($sql)) {


  $fromUser = getNameFromID1($return->messageFrom);


  $messageDate = gmdate("M d Y H:i:s",$return->messageDate);


  if($return->messageRead == "0") {
  $messageRead = "notread.jpg";
  }
  else {
  $messageRead = "read.jpg";
  }


  echo "<tr><td><img src='".$messageRead."' /></td>




  <tr><td>$fromUser</td><td><a href='viewmessage1.php?messageID=$return->messageID'>$return->messageSubject</a>
  </td><td>$messageDate</td></tr>";
}



?>
</table>

 

This is my function that i have called in the inbox code:

 

 

<?php

function getNameFromID1($userID){

 

include 'gradnetconn.php';

$query = ("SELECT CONCAT(userFirstName, ' ', userSurname) AS full_name FROM gn_users INNER JOIN gn_messages WHERE gn_users.userID = gn_messages.messageFrom");

$result = mysql_query($query);

while($row = mysql_fetch_array($result)) {

return( $row['full_name']);

}

}

?>

 

 

The wrong full_name is appearing. The full name should be 'steven steven' when its saying 'Daniel Petrie'

Really frustrated on this now :( could you give me any solution or logic?

Link to comment
Share on other sites

SELECT gn_messages.*,  
CONCAT(userFirstName, ' ', userSurname) AS full_name
FROM gn_messages 
INNER JOIN gn_users
ON gn_users.user_id = gn_messages.messageTo  (-- assuming you want the user's name who it is to. You'll need to do this join AGAIN to get the sender's name as well).
WHERE messageTo = $messageTo AND messageDeleted = '0'
ORDER BY messageDate DESC"

 

 

OH right, oh yeah i understand now! Sorry, i read it all wrong!

 

SELECT gn_messages.*,  
CONCAT(userFirstName, ' ', userSurname) AS full_name
FROM gn_messages 
INNER JOIN gn_users
ON gn_users.user_id = gn_messages.messageTo  
INNER JOIN gn_users
ON gn_users.user_id =gn_messages.messageFrom
WHERE messageTo = $messageTo AND messageDeleted = '0'
ORDER BY messageDate DESC"

 

So where you've put in the note in the query above (assuming.......etc)

I have to do the join again like above?

Link to comment
Share on other sites

If you don't already have the messageTo user's name, then yes. You need to give the table an alias, so it would be like this

SELECT gn_messages.*,  
CONCAT(to_user.userFirstName, ' ', to_user.userSurname) AS to_full_name,
CONCAT(from_user.userFirstName, ' ', from_user.userSurname) AS from_full_name,
FROM gn_messages 
INNER JOIN gn_users to_user
ON to_user.user_id = gn_messages.messageTo  
INNER JOIN gn_users from_user
ON from_user.user_id =gn_messages.messageFrom
WHERE messageTo = $messageTo AND messageDeleted = '0'
ORDER BY messageDate DESC"

Edited by Jessica
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.