2013-02-22 166 views
-2

我正在編寫安裝程序腳本,我需要檢查用戶是否輸入了正確的數據庫信息,因此需要檢查mysqli_connect是否成功。一個簡單的if-else就足夠了。如何檢查mysqli_connect是否成功?

+3

嘗試[文件](http://www.php.net/manual/en/mysqli.construct.php) 。 – 2013-02-22 16:28:16

+0

可能重複? [測試php/mysqli連接](http://stackoverflow.com/questions/3678650/testing-php-mysqli-connection) – UnholyRanger 2013-02-22 16:28:24

+0

從PHP手冊中讀取mysqli_connect – GarethL 2013-02-22 16:29:21

回答

0

像這樣:

if(! $db = mysqli_connect(...)) { 
    die('No connection: ' . mysqli_connect_error()); 
} 
2

這個怎麼樣:

<?php 
$con = mysqli_connect("localhost","my_user","my_password","my_db"); 

// Check connection 
if (mysqli_connect_errno()) 
    { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 
?>