PHP MYSQL

HOW TO CONNECT TO MYSQL IN PHP

SOME THINGS TO TAKE CARE FIRST...

3 EXTENSIONS WE CAN USE TO CONNECT TO MYSQL   * PHP Data Object (PDO) * Improved MySQL (MySQLi) * MySQL (Deprecated, removed in PHP7)

 MYSQL EXTENSIONS 

ENABLE MYSQL EXTENSION IN PHP.INI

PDO RECOMMENDED - IT SUPPORTS DATABASES OTHER THAN MYSQL.   extension=pdo_mysql

MYSQLI IS TRADITIONAL, BUT ONLY WORKS WITH MYSQL.   extension=pdo_mysqli

PDO EXAMPLE

CONNECT TO DATABASE $pdo = new PDO(   "mysql:host=HOST;".   "dbname=NAME;".   "charset=utf8", USER, PASSWORD);   PREPARE SQL $stmt = $pdo->prepare("SELECT * FROM `test` WHERE `id`<=?");   EXECUTE & FETCH RESULTS $stmt->execute([3]); $results = $stmt->fetchAll(); print_r($results);

MYSQLI EXAMPLE

CONNECT TO DATABASE $mysqli = new mysqli(HOST, USER, PASSWORD, NAME);   PREPARE SQL $result = $mysqli->query("SELECT * FROM `test` WHERE `id`<=3"); print_r($result);    FETCH RESULTS while ($row = $result->fetch_assoc()) { print_r($row); }   RELEASE RESULTS $result->close();