Will WP
Will WP

Reputation: 1237

Matching variable to two columns joined (PHP)

A previous variable from a query gave me a value $name. I need to find the user id associated with that name, however in my users table I have two fields, firstName and lastName.

I cannot explode $name as I have both cases of double names (e.g. John Eric Smith) and last names (e.g. Jan van der Worde), so my attempt was to find a way to match firstName + lastName with $name.

My attempt was this:

$drid = "SELECT id FROM users WHERE CONCAT(firstName,' ',lastName)='$name'";
$rest = mysql_query($drid);
while ($row = mysql_fetch_row($rest)) {
    $driver_id = $row[0];
} 

Unfortunately, nothing comes out as a result for $driver_id (whereas $name returns a result).

Thank you for your help!

Upvotes: 0

Views: 81

Answers (2)

Jacob Benny John
Jacob Benny John

Reputation: 71

I would suggest adding a new fullname field or using a temp table rather than using the concat, for performance reasons.

https://stackoverflow.com/a/29285246/3923450 should work though if you are looking for a temp solution

Upvotes: 1

Keep Coding
Keep Coding

Reputation: 602

Are you looking for something like this:

<?php
$drid = "SELECT id FROM users WHERE CONCAT(firstName, ' ', lastName) LIKE '%".$name."%'";
$rest = mysql_query($drid);
while ($row = mysql_fetch_row($rest)) {
    $driver_id = $row[0];
}
?>

Upvotes: 1

Related Questions