Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
FuelPHP and PDO prepared statement with named placeholders
  • Hi guys, I'm new to FuelPHP and I've been looking for ways to use PDO prepared statements using the PDO class but haven't been able to find anything yet. This is what I'm trying to do with FuelPHP following: <code>
    /* Execute a prepared statement by binding PHP variables */
    $calories = 150;
    $colour = 'red';
    $sth = $dbh->prepare('SELECT name, colour, calories FROM fruit WHERE calories < :calories AND colour = :colour');
    $sth->bindParam(':calories', $calories, PDO::PARAM_INT);
    $sth->bindParam(':colour', $colour, PDO::PARAM_STR, 12);
    $sth->execute();
    </code> Is this possible in some way? I've tried the following: <code>
    $query = \Fuel\Core\DB::query('SELECT name, colour, calories FROM fruit WHERE calories < :calories AND colour = :colour', \Fuel\Core\DB::SELECT);
    $query->param(':lang', $user->lang);
    $res = $query->execute();
    </code> I don't get to specify the datatypes of the parameters as I do in PDO with PDO::PARAM_INT, is there a way to specify datatypes of parameters so that escaping is done properly? Thanks
  • The current PDO driver is generic, and doesn't support PDO specific methods. The param() or bind() method doesn't use them as well, they simply do a string replace. Most people don't code their queries manually, but use either the Query builder or an ORM implementation, where binding isn't used.
  • Thanks for the reply WanWizard. will try to figure out if I can use the PDO in some way and "inject" the result to current fuelPHP's implementation of the PDO class. I'm working on an application with a lot of traffic so I need to have full control of queries that are launched against the DB to keep performance at a good level.
  • You can access the PDO connection created using
    // get the connection id for the default DB instance
    $db = \Database_Connection::instance()->connection();
    
    // $db now contains the PDO object
    $db->prepare('your query here');
    

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion