I've decided to swap my mysqli functions for PDO today. I was following this guide.
The code is basicaly this thing, thats pretty much exact same code from the tutorial
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
define('USER', 'my_user');
define('PASS', 'my_pass');
function dataQuery($query, $params) {
$queryType = explode(' ', $query);
// establish database connection
try {
$dbh = new PDO('mysql:host=localhost;dbname=my_db_name', USER, PASS);
$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e) {
$errorMsg = $e->getMessage();
echo $errorMsg;
}
// run query
try {
$queryResults = $dbh->prepare($query);
$queryResults->execute($params);
if($queryResults != null && 'SELECT' == $queryType[0]) {
$results = $queryResults->fetchAll(PDO::FETCH_ASSOC);
return $results;
}
$queryResults = null; // first of the two steps to properly close
$dbh = null; // second step to close the connection
}
catch(PDOException $e) {
$errorMsg = $e->getMessage();
echo $errorMsg;
}
}
$test = dataQuery('SELECT * FROM `test_table`', array(''));
echo "<pre>";
print_r($test);
If I comment out the "$dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);", I get all the results without a problem. Using it I get an empty array.
I've tried looking for a solution, but all "similar" errors seem to have other underlying problems, mostly to do with parameters. I'm not even using those yet, just trying to pass an empty array for params.