-3

I used this code to connect, This code will show error as I used password = "fd", rather I should use this password = "", I know. But I want to check whether this mysqli_connect_error() is working or not. And I found that this is not working rathe this is showing me, this error

Fatal error: Uncaught mysqli_sql_exception: Access denied for user 'root'@'localhost' (using password: YES) in C:\xampp\htdocs\php\database\insert_data.php:8 Stack trace: #0 C:\xampp\htdocs\php\database\insert_data.php(8): mysqli_connect('localhost', 'root', 'fd', 'mydb') #1 {main} thrown in C:\xampp\htdocs\php\database\insert_data.php on line 8

Tell me why this mysqli_connect_error() function now working?

<?php
    $servername = "localhost";
    $username = "root";
    $password = "fd";
    $dbname = "mydb";

    // create connection
    $conn = mysqli_connect($servername, $username, $password, $dbname);

    // check connection

    if(!$conn){

        echo "Connection failed!". mysqli_connect_error();
    }


    $sql = "INSERT INTO MyGuests (firstname, lastname, email) VALUES ('john', 'soldi', 'johnsoldi@gmail.com')";



    if(mysqli_query($conn, $sql)){
        echo "New records created successfully!";
    }else{
        echo "Error: ". $sql . "<br>" . mysqli_error($conn);
    }

    mysqli_close($conn);

?>
Dharman
  • 26,923
  • 21
  • 73
  • 125
Jonesoldi
  • 1
  • 1
  • 1
    It's throwing an exception, so you should use a try/catch block. – aynber May 18 '22 at 14:18
  • 1
    @aynber **what for?** To do `echo "Connection failed!". mysqli_connect_error();`, **seriously**? – Your Common Sense May 18 '22 at 14:19
  • @YourCommonSense For "Fatal error: Uncaught mysqli_sql_exception". Putting the connection attempt in the try and the `mysqli_connect_error` in the catch will allow them to properly check the error message. Though they should end the script if it fails instead of continuing on. – aynber May 18 '22 at 14:20
  • 1
    @aynber What do you mean, "properly check? What's improper with the current error message? And what is this "proper check" you suggest? `echo "Connection failed!". mysqli_connect_error();`? – Your Common Sense May 18 '22 at 14:22
  • 2
    @Jonesoldi mysqli_connect_error() technically works but in the recent PHP versions it's obsoleted and no more needed. Your current error message already has everything you need. You can remove the entire `if(!$conn){` condition altogether – Your Common Sense May 18 '22 at 14:27
  • But this code should print connection failed!, why this is not showing, why fatal error. – Jonesoldi May 18 '22 at 14:31
  • 2
    Because your code should never print this pointless "Connection failed". So it has been fixed – Your Common Sense May 18 '22 at 14:34
  • 1
    When connection failed, it's definitely a fatal error. So PHP merely tells you so. – Your Common Sense May 18 '22 at 14:35
  • But I was watching some tutorial on youtube, and that guy is printing this message. Thats why I tried it. – Jonesoldi May 18 '22 at 14:36
  • 2
    That guy has no clue. – Your Common Sense May 18 '22 at 14:36
  • 1
    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 May 18 '22 at 14:46
  • 1
    The point is that you _could_ use mysqli_connect_error() but there's _no point_ because the exception message is already telling you the same information. _And_ it's halting your script for you, which makes a lot of sense because it's unlikely to be able to do anything useful if it can't connect to the database. And also hopefully (if you've configured it properly) the error is being written to the PHP error log so you can keep track of such problems in your code. And you don't have to write all that extra code to make it happen. So this way is actually much better. – ADyson May 18 '22 at 15:27

0 Answers0