# PHP using PDO

**URL:** <https://www.phphelp.com/t/php-using-pdo/33273>\
**Category:** MySQL database\
**Created:** [March 28, 2021, 4:35am UTC](https://www.phphelp.com/t/php-using-pdo/33273 "2021-03-28T04:35:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Glanglais](https://www.phphelp.com/letter_avatar_proxy/v4/letter/g/46a35a/32.png) [@Glanglais](https://www.phphelp.com/u/Glanglais)\
**Post date:** [March 28, 2021, 4:35am UTC](https://www.phphelp.com/t/php-using-pdo/33273/1 "2021-03-28T04:35:42Z")

</div>

when running CLASS\_Database.php , from the address bar

i have

**Parse error** : syntax error, unexpected ‘(’ in **/home2/glanglais57/public\_html/directory/includes/class\_database.php** on line **337**

I have trouble to fix the Parse error.

I will appreciate help.  
Thank’s in advance

335- function Connect($host, $user, $pass, $name, $type =  
‘mysql’) {  
336- PDO;  
337- $this-\>connection = new ( ‘’ . $type . ‘:host=’ . $host .  
‘;dbname=’ . $name, $user, $pass );  
}

---

<div class="post-metadata">

**Author:** ![whispers](https://www.phphelp.com/letter_avatar_proxy/v4/letter/w/858c86/32.png) [@whispers](https://www.phphelp.com/u/whispers)\
**Post date:** [March 28, 2021, 5:05am UTC](https://www.phphelp.com/t/php-using-pdo/33273/2 "2021-03-28T05:05:24Z")

</div>

Not really sure what this class is all doing?

The whole syntax looks to be off as well? The odd single quotes… as well as your params out of order?

Not sure what the PDO; line does either (although could be legit? just not how I do things I guess?)

Maybe this helps:

```
$this->connection = new ($type.':host=' . $host . ';dbname=' . $user, $pass, $name);

```

I dont use a class of a function…

I often have my included DB connection file like so:

```
try{
	$dbhost = 'localhost';
	$dbname = 'test';
	$dbuser = 'root';
	$dbpass = '';
	$conn = new PDO('mysql:host=' . $dbhost . ';dbname=' . $dbname, $dbuser, $dbpass);
	$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}catch(PDOException $e) {  
	echo $e->getMessage();  
}
```

---

<div class="post-metadata">

**Author:** ![Glanglais](https://www.phphelp.com/letter_avatar_proxy/v4/letter/g/46a35a/32.png) [@Glanglais](https://www.phphelp.com/u/Glanglais)\
**Post date:** [March 28, 2021, 5:27am UTC](https://www.phphelp.com/t/php-using-pdo/33273/3 "2021-03-28T05:27:01Z")

</div>

Thank’s Whispers , for your comments. I will try your suggestion. The class id taking care or sql connection and PDO connection. I am not an expert and i have trouble to get access to my backend as i use to be and this is since update of php 7 , i am looking for help???  
getting access to my admin back office to manage wordpress is fine but it is when i try the public\_html/directory/cp/index that nothing happen else than error like  
**Deprecated** : Non-static method Registry::getInstance() should not be called statically in **/home2/glangl7/public\_html/RESSOURCESSINESVILLEDEQUEBEC.COM/directory/defaults\_setup.php** on line **242**

---

<div class="post-metadata">

**Author:** ![Strider64](https://www.phphelp.com/user_avatar/www.phphelp.com/strider64/32/3161_2.png) [@Strider64](https://www.phphelp.com/u/Strider64)\
**Post date:** [March 28, 2021, 9:56am UTC](https://www.phphelp.com/t/php-using-pdo/33273/4 "2021-03-28T09:56:03Z")

</div>

Not seeing the code I’m unsure, but the following might work?

```
// Get an instance of the Database.
// @return Database:
protected static function getInstance(): Database
{
    if (!self::$_instance) {
        self::$_instance = new self();
    }
    return self::$_instance;
}

public static function pdo(): PDO
{
    $db = static::getInstance();
    return $db->getConnection();
}

// Empty clone magic method to prevent duplication:
private function __clone() {

}

// Get the PDO connection:
protected function getConnection(): PDO
{
    // This is where you grab the connection
    // from the __construct()
    return $this->_connection; 
}

```

I’m using php 8.0 so :Database & : PDO might have to be remove for earlier versions.

---

<div class="post-metadata">

**Author:** ![Glanglais](https://www.phphelp.com/letter_avatar_proxy/v4/letter/g/46a35a/32.png) [@Glanglais](https://www.phphelp.com/u/Glanglais)\
**Post date:** [March 28, 2021, 4:49pm UTC](https://www.phphelp.com/t/php-using-pdo/33273/5 "2021-03-28T16:49:25Z")

</div>

Thank’s Strider64 , i am taking note
