使用PHP连接数据库有哪些最安全的方法?

use*_*535 11 php mysql syntax

我还是PHP和MYSQL的新手,我正在努力学习现代编码技术.我在网上找到的所有东西似乎已经过时了.

任何人都可以为我提出建议吗?如果下面的代码已经过时,我也很好奇?如果它确实过时了,你能建议更新更安全的方法吗?

<?php
    $connection = mysql_connect("localhost", "root", "");
    if (!$connection) {
        die("Oops, error happened: " . mysql_error());
    }
?>
Run Code Online (Sandbox Code Playgroud)

Sum*_*ani 10

使用PDO功能.

使用PDO进行数据库连接:

$conn = new PDO('mysql:host=localhost;dbname=myDatabase', $username, $password);
Run Code Online (Sandbox Code Playgroud)