連接到MySQL數據庫的PHP:基本知識
>
>如何在我的PHP腳本和MySQL數據庫之間建立連接?使用mysqli(面向對象):
<code class="php"><?php $servername = "localhost"; $username = "your_username"; $password = "your_password"; $dbname = "your_database_name"; // Create connection $conn = new mysqli($servername, $username, $password, $dbname); // Check connection if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Connected successfully"; //Remember to close the connection when finished: $conn->close(); ?></code>
<code class="php"><?php $servername = "localhost"; $username = "your_username"; $password = "your_password"; $dbname = "your_database_name"; try { $conn = new PDO("mysql:host=$servername;dbname=$dbname", $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(); } //Remember to close the connection when finished (though PDO handles this more automatically): ?></code>>>使用pdo:
"localhost"
"your_username"
"your_password"
"your_database_name"
Connection failed: ...
Access denied for user ...
Unknown database ...
PHP Warning: mysqli_connect(): (HY000/1045): Access denied for user ...
::root
訪問。 僅授予用戶需要執行的特定任務所需的權限。 >
定期備份數據庫,以防止數據丟失。並保護您的數據庫免於未經授權的訪問。 請記住,安全是一個持續的過程,需要持續的警惕和更新。以上是PHP連接MySQL數據庫基礎知識的詳細內容。更多資訊請關注PHP中文網其他相關文章!