Gary Gauthier
Gary Gauthier

Reputation: 195

How does one concatenate database values into a string?

I would like to string together database values into a string. Something like $a "text" $b. And then use the entire string as a variable, let's say $c.

Is it better to do this at the database level? Will php use lots of resources to do this?

Upvotes: 1

Views: 539

Answers (3)

Jon Black
Jon Black

Reputation: 16559

perfectly acceptable - calculated or derived field in mysql for example

 select
  c.firstname,
  c.lastname,
  concat(c.firstname, ' ', c.lastname) as fullname
 from
  customer c
 where
  c.cust_id = 1;

Upvotes: 3

user271586
user271586

Reputation:

Depends on the database, in mysql you can use the function CONCAT

For example, UPDATE users SET NAME=CONCAT('asd', 'asdfac') WHERE id=2;

Upvotes: 4

Your Common Sense
Your Common Sense

Reputation: 157839

No, either database or php won't notice this operation.
Better to do in php as it would be a lot readable.

Upvotes: -1

Related Questions