Home » Php » php – PDO will not execute, but works just fine in phpmyadmin

php – PDO will not execute, but works just fine in phpmyadmin

Posted by: admin February 25, 2020 Leave a comment

Questions:

I have the following code:

<?php
// ... other code...
$prepared_statement = <<< MYSQL
SELECT `Organization`, `City`, `Region`, `Country`, `Contact`, `Manager`, `Description`
FROM `businesses`
WHERE (
  (`Organization` LIKE ? OR `City` LIKE ? OR `Region` LIKE ? OR `Country` LIKE ? OR `Manager` LIKE ? OR `Description` LIKE ?)
  AND 
  (`Organization` LIKE ? OR `City` LIKE ? OR `Region` LIKE ? OR `Country` LIKE ? OR `Source` LIKE ? OR `Description` LIKE ?)
)
ORDER BY `Timestamp`;
MYSQL;
$value_list = ["Missouri","Missouri","Missouri","Missouri","Missouri","Missouri","US","US","US","US","US","US"]; // Populated through some passed in parameters, simplified to a known result here for simplicity sake.
try {
  $query = $this_pdo -> prepare();
  $query->execute($value_list); // <-- This line is where it breaks, but doesn't trigger the catch and doesn't output any error. Doing it in phpmyadmin returns 1 result.
  $result = $query->fetchAll(PDO::FETCH_ASSOC);
  return $result;
} catch (PDOException $e) {
  return ["A database problem has occurred: " . $e->getMessage()];
}

I’ve tried running the query directly in the database, replacing the “?” marks with values in the array, and it works. But execute doesn’t?

How to&Answers:

Found my problem. Simple mistake… forgot to put my prepared statement into the prepare call.

* pdo->prepare() does not return an error if it is empty, despite it being empty fails subsequent execute calls.*