How to connect to MySQL database in PHP 7


There 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> &lt;?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"; ?&gt; </code> <h3>MySQLi (object-oriented)</h3> <code> &lt;?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(); } ?&gt; </code>

<em>I recommend that you use PDO</em>


Please refer to <a href="" target="_blank"></a> for more info.

What are you looking for?