0

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.

McCuz
  • 227
  • 1
  • 12
  • 1
    Don't use this guide, [it suffers from many severe flaws](https://phpdelusions.net/pdo/common_mistakes) – Your Common Sense Jan 25 '17 at 14:00
  • As of the results you get, this is rather impossible. double check your code for typo-like errors. – Your Common Sense Jan 25 '17 at 14:05
  • 1
    Why are you passing an array with an empty string `array('')` when the query itself accepts no parameters? That would cause PDO to throw an exception related to an invalid parameter count, but you may not be seeing the message for some reason. I'm surprised this works with emulated prepares enabled anyway. – Michael Berkowski Jan 25 '17 at 14:07
  • Run it with a truly empty array for `$params`. What happens then? – Michael Berkowski Jan 25 '17 at 14:08
  • 1
    @MichaelBerkowski yeah, that's the case. I never seen it before. To my surprise, PDO doesn't throw an error but just silently returns results when emulation is on and empty result when it's off – Your Common Sense Jan 25 '17 at 14:47
  • Thanks for the answers guys. I'll try running it tomorrow and post my results. I'll definitely go over phpdelusions.net link slowly to try and understand everything. – McCuz Jan 25 '17 at 14:49
  • @YourCommonSense Very strange, I just verified it too. That seems like buggy behavior. – Michael Berkowski Jan 25 '17 at 14:53

0 Answers0