Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
How to use PDO with FuelPHP
  • Hi, I come from France, so, sorry if my english is not good.

    I would like to know how to use PDO with FuelPHP or if there is another way to create and execute queries. If you have got any documentation on the subject I will be grateful.

    Thanks for any help.
  • Depends on what you mean with "use PDO with FuelPHP".

    The DB layer uses PDO by default, so add a platform, database and credential to your <environment>/db.php. Job done.

    Or do you mean something else?
  • In fact, at the beginning I had created my requests with the DB class 
    ($result = DB:: query (' HIGH-END * FROM `users ` ')-> execute ();) for example. 

    But I met a problem at the time of realizing a fetch or a fetchAll (). These functions do not seem to be accepted by FuelPHP. So I returned to the requests using the PDO class 

    ($this->_ pdo = pdodb:: instance();
    $stmt = $this-> _ pdo-> prepare ($sql) -> execute ();
    $result = stmt-> fetchAll ();)

    Where the PDODB class contains 
    (public static function instance (connexion = ' default ', configs = array ()) {
    $db = Config:: load (' database ' connexion. '.connection .connection ');
    $db = array_merge (db, configs);
    return new PDO (db [' dsn '], db [' username '], db [' password ']);
    })

    Thank you for a help.
  • Why do you need low-level calls? That sort of defeats the purpose of using a framework.

    The DB drivers do a fetchAll by default, and return the result encapsulated in a result object. Doing it this way requires 10 lines of code where the framework only uses one.

    When you use the DB classes, you should not think in terms of low-level method calls, but in terms of what you want to achieve functionally.
  • So, if I understood well I don't need to use fetch or fetchAll, just to execute my request and the request result is an Object, is it right?
  • Correct. Something like:

    $result = DB::select()->from('users')->execute();
    foreach($result as $user)
    {
        echo $user->name;
    }
    You can find quite a few examples here: http://docs.fuelphp.com/classes/database/usage.html
  • Could I do like that?

    $sql = "Select * FROM users";
    $result = DB::query($sql)->execute();
    foreach($result as $user)
    {
        echo $user->name;
    }
  • Yeah, that's the same.

    But you should not write hardcoded SQL queries. Use the methods provided by the query builder, so you can be sure you're queries are secure and protected against SQL injections.

    The query builder supports all operations, complex expressions (using a DB::expr() object), and query binding for passing variables to queries.
  • Ok thank you very much for all these detailed explanations.


  • You're welcome, happy coding!

Howdy, Stranger!

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

In this Discussion