0

I am trying to see if this code works so that no Fatal Error will come out. I am purposely trying to make it show an error. I don't have a database called "aaaa"

$conn = mysqli_connect("localhost", "root", "", "aaaa");
if($conn ->connect_error){
    die("Connection Error");
}

But is something wrong here? Because it is not showing the message. I was trying to make the same result as this try/catch code here.

try{
    $conn = mysqli_connect("localhost", "root", "", "aaaa");
}
catch (Exception $e){
    die("Connection Error");
}

And this one is properly working. I am searching how to catch errors in Mysqli and the 1st code are the results that I get but somehow it is not working for me. Did I do something wrong? Because it throws Fatal Error instead of the message "Connection Error".

Barmar
  • 741,623
  • 53
  • 500
  • 612
  • If `mysqli_connect()` throws an exception, normal code flow gets interrupted, so the `if()` that follows does not even run. That isn't a particularity of this extension, it's how exceptions work in general. – Álvaro González Mar 02 '22 at 15:40
  • @ÁlvaroGonzález so you can only use try/catch to catch those exception? – RONALDO JR. ARNIBAL Mar 02 '22 at 15:41
  • 1
    You can disable mysqli exception mode. But then `mysqli_connect()` returns `false` when it fails, it doesn't return an object that you use `->connect_error` with. You have to call `mysqli_connect_error()`. – Barmar Mar 02 '22 at 15:48
  • 1
    If you opt for exceptions, that's the way it is. Of course, you don't need to catch every individual exception. You can change the [report mode](https://www.php.net/manual/en/mysqli-driver.report-mode.php) to `MYSQLI_REPORT_OFF` but I honestly think exceptions are easier to use (and more difficult to forget about). – Álvaro González Mar 02 '22 at 15:48
  • 1
    Do you realize that writing die("Connection Error"); makes absolutely no sense and such a code should never be written? Such a message doesn't help a site visitor, least a site owner|programmer. – Your Common Sense Mar 02 '22 at 15:53
  • @ÁlvaroGonzález please, please, please, die("Connection Error"); is absolutely NOT the way. – Your Common Sense Mar 02 '22 at 15:54
  • in order to prevent a fatal error to come out you must configure your PHP to disable errors coming out. Sounds logical? – Your Common Sense Mar 02 '22 at 15:56
  • according to the [https://www.php.net/manual/fr/function.mysqli-connect.php](report mode) the mysqli_connect funtion may return false , you can try and change the if ( $conn === false ) – R. Martin Mar 02 '22 at 15:56
  • @YourCommonSense I'm just testing this first so I'm using die("Connection Error"); I just wanna know if the error was properly caught or not. – RONALDO JR. ARNIBAL Mar 02 '22 at 15:58
  • There is absolutely no point in testing with die("Connection Error");. What you need is a fatal error, not this – Your Common Sense Mar 02 '22 at 16:00
  • I just wanna know where the Fatal Error is happening just like how you put console messages when checking for errors. Well, that's how I usually do it when checking errors. – RONALDO JR. ARNIBAL Mar 02 '22 at 16:01
  • 1
    So with your code you have NOT A SLIGHTEST IDEA where this pointless "connect error" is coming from. While with a fatal error it is written RIGHT IN THE ERROR MESSAGE, exact file name and line number. – Your Common Sense Mar 02 '22 at 16:03
  • Please read here: https://phpdelusions.net/articles/error_reporting – Your Common Sense Mar 02 '22 at 16:04
  • Dude... I'm literally just starting to learn how to use Mysqli and php.... Can you please understand that I'm a noob at this thing? – RONALDO JR. ARNIBAL Mar 02 '22 at 16:08
  • Yes and I deleted the inappropriate statement. So instead I offer you resource to learn – Your Common Sense Mar 02 '22 at 16:18
  • Ronaldo, it's difficult to explain some concepts in comments section. What YCS is trying to tell you is that you are not helping yourself learn PHP if you ignore standard errors and put vague messages in your code. Remove the `die` as you don't need it at all. You only need to listen to what PHP tells you. Debug statements like die might be useful if you can't use the debugger and you want to see which line of code is executed, but not when it comes to standard errors. – Dharman Mar 02 '22 at 16:19
  • It looks like you are using some really bad tutorial. I highly encourage you to find something better. If you are only starting to learn PHP then you should learn PDO instead of mysqli. PDO is much easier and more suitable for beginners. Start here https://phpdelusions.net/pdo & https://websitebeaver.com/php-pdo-prepared-statements-to-prevent-sql-injection. Here are some good video tutorials https://youtu.be/2eebptXfEvw & https://www.youtube.com/watch?v=sVbEyFZKgqk&list=PLr3d3QYzkw2xabQRUpcZ_IBk9W50M9pe- – Dharman Mar 02 '22 at 16:19
  • yes I know how useful standard errors are and I understand what YCS is saying. I was also testing on how to trigger fatal errors and multiple of them and make it readable and so I used die instead of letting it be so instead of reading 2 lines of fatal error, I just need to read a brief message. – RONALDO JR. ARNIBAL Mar 02 '22 at 16:24

0 Answers0