0
function fullname($id){
$query = mysql_query("SELECT firstname, lastname FROM users WHERE id = '$id'");
$row = mysql_fetch_array($query);
$full_name = $row["firstname"] . " ".$row["lastname"];

return $full_name;
}

I want to grab firstname and lastname, put them together, and then echo it back, is this right?

2
  • 1
    Have you tried? Does it work? If yes, it is probably right. Just a note: If the id is an integer, you should not enclose it in single quotes: "SELECT firstname, lastname FROM users WHERE id = $id". Commented Sep 26, 2010 at 11:33
  • 1
    It should work if your $query work. But if you ask is it works, it's it doesn't work, nop ? Commented Sep 26, 2010 at 11:34

2 Answers 2

1

if id field is an integer you do not have to use like 'id'

this is the correct line of code

$query = mysql_query("SELECT firstname, lastname FROM users WHERE id = $id");
Sign up to request clarification or add additional context in comments.

1 Comment

Better make sure $id is in fact a number first. if(!is_numeric($id)) {return false;} Though if id is unsigned, is_int($id) might be better.
1

why don't you join firstname and lastname in the sql?

$query = mysql_query("SELECT concat(firstname, ' ',lastname) as 'Name' FROM users WHERE id = $id");

normally this should also work

$query = mysql_query("SELECT firstname + ' ' + lastname as 'Name' FROM users WHERE id = $id");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.