-2

I'm creating a new PHP page and I have a variable which consists out of the addition of two fields of my MySQL database. I would like to find which row corresponds to the variable, which consists of a combination of two fields.

I was unable to find any answers online, and when I did, none of them seem to work.

Database example:

 ID | Name 1  | Name 2
----+---------+--------
 1  | john 1  | doe
 2  | jane    | doe
 3  | john    | doe
 4  | john    | doe 1

My variable which I ought to match to a row is $var = "john doe 1";, and splitting my variable is not an option.

I expect the output of said SQL statement to be the row with ID = 4.

EDIT: I do not believe this to be a duplicate. I'm not having any problems with the CONCAT clause itself, I was consulting about which clause to use and how to use it, not how to fix my current one.

jkdev
  • 11,360
  • 15
  • 54
  • 77
AMG
  • 1
  • 2

1 Answers1

-3

You seem to want:

where concat(name1, ' ', name2) = 'john doe 1'

Note: It is tempting to write:

where concat(name1, ' ', name2) = '$var'

However, you should learn to use parameters when passing constant values into a query.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786
  • 2
    3 reasons for downvote: 1. You answered a question which is a duplicate. 2. You used `concat` instead of `CONCAT_WS`. 3. You didn't show how to use prepared statements and left that part very unclear. – Dharman Jul 14 '19 at 10:13