WEB DEVELOPMENT

MySQL Connect

Before data can be accessed in the MySQL database, a connect needs to be made to the server. Earlier versions of PHP used the MySQL extension which is now depreciated. In PHP there are three ways of working with MySQL: MySQLi (object-oriented), MySQLi (procedural) and PDO (PHP Data Objects). An comprehensive write up comparing MySQLi and PDO is available here: https://websitebeaver.com/php-pdo-vs-mysqli.

MySQLi Object-Oriented


<?php

mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
try {
    $mysqli = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
    $mysqli->set_charset(DB_CHAR);
} catch(Exception $e) {
    error_log($e->getMessage());
    exit('MySQLi: Error connecting to database.'); 
}

?>


<?php

    $mysqli->close(); 
    
?>