Skip to main content
make constants consistent between code and text
Source Link

Kind of an old post, but I had to add something where I saw this line

$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

Which seems to me like you had hard-coding the settings, right after this line:

$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 

Which means, whatever (relating to ATTR_MODEATTR_ERRMODE, ATTR_DEFAULT_MODEATTR_DEFAULT_FETCH_MODE) the user sets as $options in the constructor will be overriding by the setAttribute method. Which in my opinion seems pointless. I would suggest changing the constructor to something like:

...

private $defaultPdoAttr = [
    \PDO::ATTR_EMULATE_PREPARES => FALSE,
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
];
     
public function __construct($dsn, $username, $password, array $driverOptions = [])
{
    if (!$driverOptions) {
        $driverOptions = $this->defaultPdoAttr;
    }  
   
    $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $driverOptions );

...

Kind of an old post, but I had to add something where I saw this line

$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

Which seems to me like you had hard-coding the settings, right after this line:

$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 

Which means, whatever (relating to ATTR_MODE, ATTR_DEFAULT_MODE) the user sets as $options in the constructor will be overriding by the setAttribute method. Which in my opinion seems pointless. I would suggest changing the constructor to something like:

...

private $defaultPdoAttr = [
    \PDO::ATTR_EMULATE_PREPARES => FALSE,
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
];
     
public function __construct($dsn, $username, $password, array $driverOptions = [])
{
    if (!$driverOptions) {
        $driverOptions = $this->defaultPdoAttr;
    }  
   
    $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $driverOptions );

...

Kind of an old post, but I had to add something where I saw this line

$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

Which seems to me like you had hard-coding the settings, right after this line:

$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 

Which means, whatever (relating to ATTR_ERRMODE, ATTR_DEFAULT_FETCH_MODE) the user sets as $options in the constructor will be overriding by the setAttribute method. Which in my opinion seems pointless. I would suggest changing the constructor to something like:

...

private $defaultPdoAttr = [
    \PDO::ATTR_EMULATE_PREPARES => FALSE,
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
];
     
public function __construct($dsn, $username, $password, array $driverOptions = [])
{
    if (!$driverOptions) {
        $driverOptions = $this->defaultPdoAttr;
    }  
   
    $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $driverOptions );

...
deleted 48 characters in body
Source Link
samayo
  • 345
  • 1
  • 6
  • 15

Kind of an old post, but I had to add something where I saw this line

$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

Which seems to me like you had hard-coding the settings, right after this line:

$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 

Which means, whatever (relating to ATTR_MODE, ATTR_DEFAULT_MODE) the user sets as $options in the constructor will be overriding by the setAttribute method. Which in my opinion seems pointless. I would suggest changing the constructor to something like:

...
         
private $defaultPdoAttr = [
            \PDO::ATTR_EMULATE_PREPARES => FALSE,
            \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
            \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
        ];
     
    public function __construct($dsn, $username, $password, array $driverOptions = [])
    {
        if (!$driverOptions) {
            $driverOptions = $this->defaultPdoAttr;
        }  
   
    $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $driverOptions ); 

...

Kind of an old post, but I had to add something where I saw this line

$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

Which seems to me like you had hard-coding the settings, right after this line:

$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 

Which means, whatever (relating to ATTR_MODE, ATTR_DEFAULT_MODE) the user sets as $options in the constructor will be overriding by the setAttribute method. Which in my opinion seems pointless. I would suggest changing the constructor to something like:

...
        private $defaultPdoAttr = [
            \PDO::ATTR_EMULATE_PREPARES => FALSE,
            \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
            \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
        ];
     
    public function __construct($dsn, $username, $password, array $driverOptions = [])
    {
        if (!$driverOptions) {
            $driverOptions = $this->defaultPdoAttr;
        }  
   
    $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $driverOptions ); 

Kind of an old post, but I had to add something where I saw this line

$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

Which seems to me like you had hard-coding the settings, right after this line:

$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 

Which means, whatever (relating to ATTR_MODE, ATTR_DEFAULT_MODE) the user sets as $options in the constructor will be overriding by the setAttribute method. Which in my opinion seems pointless. I would suggest changing the constructor to something like:

...
 
private $defaultPdoAttr = [
    \PDO::ATTR_EMULATE_PREPARES => FALSE,
    \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
    \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
];
     
public function __construct($dsn, $username, $password, array $driverOptions = [])
{
    if (!$driverOptions) {
        $driverOptions = $this->defaultPdoAttr;
    }  
   
    $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $driverOptions );

...
Source Link
samayo
  • 345
  • 1
  • 6
  • 15

Kind of an old post, but I had to add something where I saw this line

$this->datab->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
$this->datab->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);

Which seems to me like you had hard-coding the settings, right after this line:

$this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $options); 

Which means, whatever (relating to ATTR_MODE, ATTR_DEFAULT_MODE) the user sets as $options in the constructor will be overriding by the setAttribute method. Which in my opinion seems pointless. I would suggest changing the constructor to something like:

...
        private $defaultPdoAttr = [
            \PDO::ATTR_EMULATE_PREPARES => FALSE,
            \PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
            \PDO::ATTR_DEFAULT_FETCH_MODE => \PDO::FETCH_ASSOC
        ];
     
    public function __construct($dsn, $username, $password, array $driverOptions = [])
    {
        if (!$driverOptions) {
            $driverOptions = $this->defaultPdoAttr;
        }  
   
    $this->datab = new PDO("mysql:host={$host};dbname={$dbname};charset=utf8", $username, $password, $driverOptions );