How to check if mysqli_connect was successful or n

2020-05-09 22:37发布

I'm writing an installer script and I need to check if the user entered correct database information and therefore need to check if mysqli_connect was successful or not. A simple if-else will suffice.

标签: php mysqli
2条回答
老娘就宠你
2楼-- · 2020-05-09 23:24

Like so:

if( ! $db = mysqli_connect(...) ) {
    die('No connection: ' . mysqli_connect_error());
}
查看更多
姐就是有狂的资本
3楼-- · 2020-05-09 23:42

What about this:

<?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();
    }
?>
查看更多
登录 后发表回答