How to check if mysqli_connect was successful or not?

前端 未结 2 840
清歌不尽
清歌不尽 2021-01-29 11:32

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 simp

相关标签:
2条回答
  • 2021-01-29 11:58

    Like so:

    if( ! $db = mysqli_connect(...) ) {
        die('No connection: ' . mysqli_connect_error());
    }
    
    0 讨论(0)
  • 2021-01-29 12:09

    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();
        }
    ?>
    
    0 讨论(0)
提交回复
热议问题