When I run this query in Google Cloud Shell it works fine and returns 5 rows of data as expected:
SELECT * FROM tblData WHERE Address LIKE '%123 Queen%' LIMIT 5;
But when I pass in the same search term 123 Queen
to this parameterized PDO query it does not return any results:
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$stmt = $conn->prepare("SELECT * FROM tblData WHERE Address LIKE :term LIMIT 5");
$stmt->execute(array(':term' => '%'.$_GET['term'].'%'));
What am I doing wrong?
尝试如下操作: SELECT * FROM tblData WHERE Address LIKE CONCAT('%',:term,'%') LIMIT 5
My original question asked why this parameterized PDO query did not return any records:
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$stmt = $conn->prepare("SELECT * FROM tblData WHERE Address LIKE :term LIMIT 5");
$stmt->execute(array(':term' => '%'.$_GET['term'].'%'));
$array = array();
foreach(new TableRows(new RecursiveArrayIterator($stmt->fetchAll())) AS $x) {
$array[] = $x;
}
As it turns out, there is absolutely nothing wrong with the above code. My error was that I was referencing the wrong variable name in the json_encode
statement below. I mistakenly referred to array
, when I should have referred to $array
like this:
$array = $array ?: array('Not Found');
echo json_encode($array);
I am providing this answer at the request of @LundinCast.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.