-1

I'm trying to find a list of people's names that match a search. My php:

require "accessControl.php";
require "sqlLink.php";

$string = "%dan%";

// create a prepared statement
if ($stmt = $link->prepare("SELECT full_name FROM (SELECT *, CONCAT(firstname," ",lastname) AS full_name FROM users) tmp WHERE full_name LIKE ?")) {

    $stmt->bind_param("s", $string);

    $stmt->execute();

    $stmt->bind_result($result);

    $stmt->fetch();

    $stmt->close();
}

echo $result;

This just gives a 500 error, but when I replace the query with "SELECT lastname FROM users WHERE firstname LIKE ?", it works fine. I spent over an hour searching for a solution, but I'm pretty confused.

2 Answers2

3

Replace condition in your if with this:

$stmt = $link->prepare("SELECT full_name FROM (SELECT *, CONCAT(firstname,' ',lastname) AS full_name FROM users) tmp WHERE full_name LIKE ?")

Here, you just need to replace " " with ' ' in your CONCAT() function because that would break your entire string which also starts and ends with ".

Amit Merchant
  • 1,045
  • 6
  • 21
2

First don't use " " inside duoble quoted string .. becuase break the string continuity. second you could use concat for buil string with wilchar

$string = "dan";
$link->prepare("SELECT full_name FROM (
                SELECT *, CONCAT(firstname,' ',lastname) AS full_name FROM users
              ) tmp WHERE full_name LIKE concat( '%', ?, '%'))");
ScaisEdge
  • 131,976
  • 10
  • 91
  • 107