INFO
How to connect to MySQL database in PHP 7
2019-12-04|Views:2177There are two <b>current</b> procedures to enable you to connect php7 to your database.
<ul> <li>MySQLi (object-oriented)</li> <li>PHP Data Objects - PDO (object-oriented)</li> </ul>MySQLi (object-oriented)
<code> <?php $servername = "localhost"; $username = "username"; $password = "password"; <br> // Create connection $conn = new mysqli($servername, $username, $password); <br> // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; ?> </code> <h3>MySQLi (object-oriented)</h3> <code> <?php $servername = "localhost"; $username = "username"; $password = "password"; <br> try { $conn = new PDO("mysql:host=$servername;dbname=myDB", $username, $password); // set the PDO error mode to exception $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); echo "Connected successfully"; } catch(PDOException $e) { echo "Connection failed: " . $e->getMessage(); } ?> </code><em>I recommend that you use PDO</em>
<br>Please refer to <a href="https://www.w3schools.com/php/php_mysql_connect.asp" target="_blank">https://www.w3schools.com/php/php_mysql_connect.asp</a> for more info.