Browse Categories
Algorithms 3
Apache 1
Arrays 5
COM for Windows 2
CURL 1
Classes & Objects 2
ClibPDF 1
CrackLib 1
DOM XML 4
Directory 2
Error Handling & Logging 1
FTP 1
Filesystem 9
Forms Data Format 6
FrontBase 2
Function Handling 4
GMP 4
Gettext 1
HTTP 3
Image 7
Informix 4
Ingres 5
InterBase 5
LDAP 9
Ming 4
Miscellaneous 2
MySQL 12
PHP Java Integration 1
PostgreSQL 4
Regular Expressions 3
String Manipulation 12
Time and Date 6
Variable 3
XML Related 3
Zlib 3
dBase 2
Featured Snippet
MySQL Connection Example
MySQL PHPA clean, reusable MySQL connection snippet using PDO for modern PHP applications.
PHP
<?php
$host = 'localhost';
$db = 'my_database';
$user = 'root';
$pass = 'secret';
$charset = 'utf8mb4';
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$options = [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
PDO::ATTR_EMULATE_PREPARES => false,
];
try {
$pdo = new PDO($dsn, $user, $pass, $options);
} catch (\PDOException $e) {
throw new \PDOException($e->getMessage(), (int)$e->getCode());
}
?>