connecting database PDO code example
Example 1: pdo db connection
<?php
$pdo = new PDO('mysql:host=localhost;dbname=databasename', 'username', 'password');
?>
Example 2: php mysql connect pdo
$host = 'localhost';
$user = 'root';
$pw = '';
$dbname = 'database';
$dsn = "mysql:host=$host;dbname=$dbname";
$options = [PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'];
$db = new PDO($dsn, $user, $pw, $options);
$db -> setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);
$qry = 'select * from table;';
$result = $db -> query($qry);
while ($row = $result -> fetch()) {
$id = $row[];
}