Is is possible to set a default PDO fetch mode?

前端 未结 2 671
无人共我
无人共我 2020-11-28 08:46

Before I retrieve data I always have to type:

$STH->setFetchMode(PDO::FETCH_OBJ);

In the interest of making my code more readable it wou

相关标签:
2条回答
  • 2020-11-28 09:01
    $connection = new PDO($connection_string);
    $connection->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
    
    0 讨论(0)
  • 2020-11-28 09:12
    $dsn = 'mysql:host='.$db_server.';dbname='.$db_name.';port='.$db_port;
    $driver_options = array(
       PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'utf8'",
       PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
       PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_OBJ,
    );               
    $dbh = new PDO( $dsn, $db_user, $db_pass, $driver_options );
    
    0 讨论(0)
提交回复
热议问题