@mysql_connect and mysql_connect

后端 未结 5 1482
野趣味
野趣味 2021-02-06 01:22

I have no problem connecting to a database using PHP however in some scripts I have tested, I have seen a small difference in the connect command.

What is the differenc

相关标签:
5条回答
  • 2021-02-06 01:55

    The @ supresses warnings http://php.net/manual/en/language.operators.errorcontrol.php use it wisely

    0 讨论(0)
  • 2021-02-06 01:58

    The @ symbol in front of a function silences it. Meaning, you won't get any types of error messages when executing it, even if it fails. So I suggest: don't use it

    In addition as @AlexanderLarikov said, don't use mysql_* anymore, the community has started to depreciate that function.

    0 讨论(0)
  • 2021-02-06 02:09

    It is the/an error control operator. It simply allow you to suppress the error.

    I would suggest that you omit it in your code.

    From documentation:

    Currently the "@" error-control operator prefix will even disable error reporting for critical errors that will terminate script execution. Among other things, this means that if you use "@" to suppress errors from a certain function and either it isn't available or has been mistyped, the script will die right there with no indication as to why.

    0 讨论(0)
  • 2021-02-06 02:12

    That is an error suppression mechanism. So, say there was an an error when you tried to connect, PHP would silently ignore it rather than displaying/logging it (depending on your settings).

    I personally think it bad practice to use this, as, in my opinion, you should write your code to handle errors, not just silently discard them.

    0 讨论(0)
  • 2021-02-06 02:12

    If don't use any option something like this;

    if ("I'm just making test on my srv") {
       error_reporting(E_ALL);
    } else {
       error_reporting(0);
    }
    

    Then, it could be recommended for this situation;

    $conn = @mysql_connect(...);
    if ($conn === false) {
       // handle error
    }
    
    Or;
    
    @mysql_connect(...) or die("Could not connect to ...");
    

    So, @ suppresses the error if it exist at the line "where the suppressible function is used".

    // Suppressible? Yes, cos you can not apply @ to die, exit, eval ... functions if these are structural functions.

    0 讨论(0)
提交回复
热议问题