<?php
/* Provoke an error -- bogus SQL syntax */
$stmt = $dbh->prepare('bogus sql');
if (!$stmt) {
echo "\nPDO::errorInfo():\n";
print_r($dbh->errorInfo());
}
?>
$mysqli = new mysqli('localhost', 'xxxx', 'xxxxx', 'xxxxxx');
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
$sql = $mysqli->query("SELECT * FROM table WHERE id = 5 LIMIT 1");
$row = $sql->fetch_assoc(); // Skal bare hente en rad, så trenger ikke noen loop.
echo $row['id'];
$mysqli->close();
class Applicationhelper {
public $dbh;
// ...
public function __construct() {
date_default_timezone_set('Europe/Oslo');
$dsn = 'mysql:host=localhost;dbname=xxx;charset=utf8';
$username = 'yyy';
$password = 'zzz';
$this->dbh = new PDO($dsn, $username, $password);
$this->dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
// ...
}