Andrea_86
Andrea_86

Reputation: 527

mysqli in PDO constructor

Hy everyone, i'm looking to a code wrote from another student, and he used the PDO abstract lever for the connection to the database.

Now i've problem with mysql function, for example with fetch_assoc (i think why it is implemented only in mysqli). There's a way to connect with PDO and mysqli both? something like:

new PDO("mysqli:host=$this->hostname;dbname=$this->dbname", $this->username, $this->password);

adding the "i" in mysql:host

Or there's something similar at mysqli::fetch_assoc() in mysql functions?

Upvotes: 0

Views: 424

Answers (1)

aksu
aksu

Reputation: 5235

No way to use PDO and MySQLi together, but there is an option to choose fetch style in PDO. You can see those styles in here.

So, lets get to the example:

$result = $sth->fetchAll(PDO::FETCH_ASSOC); // $sth = your statement
foreach($result as $row) {
   echo $row['name'];
}

Now the $result contains query results as associative array.

The answer was more simpler than you thought, wasn't it?

Upvotes: 2

Related Questions