tags:

views:

42

answers:

2

I have a database that has a users first and last name. Some cases i display the users full name, but in other cases i just show the first name. i have a function that gathers user information.

within the function, i use an sql query to gather the first and last name, but i also concat the first and last name and label it full_name.

Example..

$q = "SELECT first_name, last_name, CONCAT_WS(' '. first_name, last_name) AS full_name 
                  /* A lot of other information */ 
FROM users WHERE user_id=$user_id";

My question is, is this too much, is it unnecessary to get the same data in different forms straight out of the database, or would it be better to just take the first and last name from the database and then concat afterward in php like so...

echo $user['first_name'].' '.$user['last_name'];
A: 

It doesn't really matter. Setting one variable takes a small fraction of a second either way. If you're expecting lots of requests, then setting it in MySQL will be faster, as when you do it in PHP, it has to search for the array key in the returned array and set that value to a variable. If you set it in MySQL, you just use the value straight out of the array without the in-between steps.

Unless you're getting a HUGE volume of people requesting that data however, you won't notice a difference.

BraedenP
+3  A: 

I would just use the database for information storage and retrieval as much as possible, and leave things like the "full name" (which is really probably used only for display purposes) to the application, i.e. php logic.

Brad
Its a matter of opinion, and I agree with this person -- IMHO, its better to handle the formatting at the code level. PHP gives you a **wider** variety of string manipulation functions.
Salman A