0

Hello,

I have some problem with php functions can anyone help me out with this

<?php
  $db_host ="localhost";
    $db_user ="my_cms";
    $db_pass = "secret";
    $db_name = "mypractice";
    $connection = mysqli_connect($db_host, $db_user, $db_pass, $db_name);

        if(mysqli_connect_errno()) {
            die("SQL Connection Failed: " .
                mysqli_connect_error() .
                "(" . mysqli_connect_errno() . ")"
            );  
        }
?>

did'nt understood about the if condition and why the errno function is used again and if we had already written in if condition why we written again like we can use like this if(mysqli_connect_errno()) {die("SQL Connection Failed:");} lots of confusion can anyone help me out

Thank You

5
Contributors
5
Replies
30
Views
2 Years
Discussion Span
Last Post by Zagga
0

Hi,

with mysqli_connect_errno() you check if there is an error, if yes then, with the same function, you get the error code, which is an integer, while with mysqli_connect_error() you get a string that describes the error.

The error code allows you to easily create a switch statement to perform specific operations, for example:

switch(mysqli_connect_errno())
{
    # unknown database
    case 1049:
        # mail admin, fallback connection
        break;

    # success
    case 0:
        # do not log, connection is fine
        break;

    default:
        # log normally
}

You can find an error list here:

Now, a MySQL error is composed by three elements:

ERROR 1049 (42000): Unknown database 'db_test'
  • the error code which is an integer
  • the SQLSTATE which is a string of five alphanumeric characters
  • the error message, which is a string describing the error

The MySQLi API does not return the SQLSTATE with the connections errors, you get them only with the queries, for example:

# wrong database
$mysqli = @new mysqli('localhost', 'user', 'pass', 'no_database');

if($mysqli->connect_errno)
{
    echo $mysqli->connect_errno . ': ' . $mysqli->connect_error;
    die();
}

# wrong query, replace `no_table` with `dual`
$mysqli->query("select 'abc' as row from no_table");

if($mysqli->errno)
{
    echo $mysqli->errno . ' ['. $mysqli->sqlstate .']' . ': ' . $mysqli->error;
    die();
}

If still in doubt, the SQLSTATE is a standard to define SQL errors. More info here:

0

The if condition here checks for an error and prints it to screen if it exists within the die(). Not sure what you're not understanding. die() halts all execution like exit(). Not pretty - so perhaps not the best way to implement in a production site. Easy for development though. My 2p.

0

@diafol well sir what i am not understating is about die function what it does and that you explained to me like it do same as the function do like exits if the connection is failed do nothing below just gives an error.

the thing i am not understanding is like we write

if(mysqli_connect_errno()) { die("the connection failed ")}

this is a bit understandable to me

if(mysqli_connect_errno()) {
            die("SQL Connection Failed: " .
                mysqli_connect_error() .
                "(" . mysqli_connect_errno() . ")"
            );  
        }

now by looking at this statement why we are concatinating mysqli_connect_errorno() again and along with mysqli_connec_error() and it is concatenated like "(" . mysqli_connect_errno() . ")" confused ??

can you explain these to me pleasethese are not understood that why we are using if we had already desined an error in if prenthesis.

0

This is what each line of the code does, written in English.

if there is an error
stop the script and print "SQL Connection Failed"
also print a message explaining the error
also print the error number in brackets
end of DIE statement
end of IF statement

You don't really need to print the last mysqli_connect_errno, but as cereal explained it can be helpful to handle the error.

It's easier to understand if the DIE statement is written on 1 line.

if(mysqli_connect_errno()) {
    die("SQL Connection Failed: " . mysqli_connect_error() . "(" . mysqli_connect_errno() . ")");
}

Edited by Zagga

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.