0

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.

Mogsdad
  • 44,709
  • 21
  • 151
  • 275
Codefoe
  • 31
  • 1
  • 1
  • 5

2 Answers2

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
1

Like so:

if( ! $db = mysqli_connect(...) ) {
    die('No connection: ' . mysqli_connect_error());
}
Sammitch
  • 30,782
  • 7
  • 50
  • 77