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.
Asked
Active
Viewed 1.2k times
0
-
3Try the [documentation](http://www.php.net/manual/en/mysqli.construct.php). – Burhan Khalid Feb 22 '13 at 16:28
-
possible duplicate? [testing php/mysqli connection](http://stackoverflow.com/questions/3678650/testing-php-mysqli-connection) – UnholyRanger Feb 22 '13 at 16:28
-
read mysqli_connect from the PHP manual – GarethL Feb 22 '13 at 16:29
2 Answers
5
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();
}
?>

Tunaki
- 132,869
- 46
- 340
- 423

Ricardo Moreno Martinez
- 51
- 1
- 3
1
Like so:
if( ! $db = mysqli_connect(...) ) {
die('No connection: ' . mysqli_connect_error());
}

Sammitch
- 30,782
- 7
- 50
- 77