0

Hi I cant get my query to return any values from database. I have this function:

 private function Query($sql, $arg){
    require "conection.php"; 
    $q = $db->prepare($sql);
    $q->execute($arg);
    $result = $q->get_result();
    return $result;
}

I've tried many different PDO statements but all give the same error code [Call to undefined method PDOStatement] I've tried binding results, fetch all... etc but nothing works.. This is my conection.php:

$host = "127.0.0.1";
$dbname = "truckla";
$username = "root";
$password = "123456";
try{
    $db = new PDO("mysql:host=$host;dbname=$dbname", $username, $password);
   }catch(Exception $e){die("Fatal error: ".$e->getMessage());}

This is the following query and how Im calling it:

$result = $this->Query("SELECT OrderId,Products,Quantis,Description,RecvrName,AddressInfo,GPS,TimeStart,TimeSpec FROM orders WHERE IsPrivate LIKE 'false' AND IsComplete LIKE 'false' AND StillOpen LIKE 'true' AND CanShow LIKE 'true' AND TruckType LIKE '%?%' OR ((GroupId LIKE '%?%' OR DriversId LIKE '%?%') AND IsComplete LIKE 'false' AND StillOpen LIKE 'true' AND CanShow LIKE 'true')", array($trucktype,$groupsId,$driverId));

The query works fine in phpMyAdmin and I can get the values if i call it like this:

$result = mysqli_query($conn , $sql);

But I dont trust in escaping bad chars and just run the query, how can I call the query and get the values! Is it a configuration error because no PDO statement is valid... Im using PHP 7.2.10, MySQL and XAMPP 3.2.2 thanks in advance!

Gabriel
  • 970
  • 7
  • 20
  • Post the line of code where this error actually happens. – Alex Howansky Jan 18 '20 at 21:08
  • `get_result`? Try calling something like `fetch` or `fetchAll` or some other method that a `PDOStatement` supports. Read the manual at https://www.php.net/manual/en/pdostatement.fetchall.php. – Booboo Jan 18 '20 at 21:51
  • The error happens when calling get_result and its the same error when i call fetch or fetchAll – a161803398874 Jan 19 '20 at 21:13
  • Hi there I just made it work! I was debugging my code incorrecly... and fetchAll() was indeed working correclty but returned an empty array... that other problem was because the use of wildcards in the prepared statement. i fixed it by appending the wildcard when passing the arguments to the prepared statement – a161803398874 Jan 19 '20 at 22:15
  • `$wildcardVar = "%".$normalVar."%":` – a161803398874 Jan 19 '20 at 22:16

0 Answers0