-1

I am creating a connection between database and PHP and when I add the wrong username( actually, real username is root and I added rsdsoot) but still showing me connected instead of showing me an error. any reason for that?

$servername = "localhost";
$username = "rsdsoot";
$password = "";

$conn = mysqli_connect($servername, $username, $password);

if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
WebQoda
  • 7
  • 1
  • Your success/failure check is incorrect. `mysqli_connect()` will return an object regardless of the result and so `!$conn` will never trigger ... Lookup the manual on php.net; the very first example there shows how to handle this. – Narf Jul 16 '19 at 18:52
  • It's working properly If I add the wrong host or add any password but if I add the wrong username it's not giving me any error. – WebQoda Jul 16 '19 at 19:16
  • @WebQoda can you do a manual check if that user you put there actually exists or not ? (Try MySqlWorkBench). – Marcus V. B. Siqueira Jul 16 '19 at 19:19
  • I can't reproduce this error. If I use invalid username I get an exception. – Dharman Jul 16 '19 at 19:22
  • Well, if you get an exception you can try catch to get to the solution. – Marcus V. B. Siqueira Jul 16 '19 at 19:25
  • I have made a answer with a try catch approach and a link to the solution I use on my Project. If you have doubts, just ask away, and if you need examples of how using my class, there are plenty on the project I maintain on git hut, the link is on the answer. – Marcus V. B. Siqueira Jul 16 '19 at 19:32
  • the only root user is available inside MySQL but the username I added is not available. I suggest please try also on your side you will know about my concern. – WebQoda Jul 16 '19 at 19:32
  • @Dharman at least it should give me an error that I am using a wrong username or any other connection error. – WebQoda Jul 16 '19 at 19:36
  • @WebQoda Have you already read the link I posted above? Did you add the line of code before your `mysqli_connect`? Do you have all errors enabled? – Dharman Jul 16 '19 at 19:38
  • @Dharman I am not sure about all errors. but I am getting a few errors. let me check that also. – WebQoda Jul 16 '19 at 19:44
  • yes, all errors are enabled. yes, I read that link but still confused. – WebQoda Jul 16 '19 at 20:02

1 Answers1

-1

Try this, an approach with try catch:

public function connect($dbhost, $dbuser, $dbpass) 
{
    try 
    {
        $cxn = @mysqli_connect($dbhost, $dbuser, $dbpass);
    } 
    catch ( Exception $e )
    {
        print 'Could not connect to the mysql server: '. $e->getMessage() .'\n';
        exit($e->getMessage());
    }
}

Additionaly, I have build a class to easy the comunication with the MySql database and make it easier to execute queries. Try it out. It's on my OpenSource project.

MySqlManager class PHP

  • 2
    Why do you silence errors? Why do you catch the exception? Why is it in a class method? – Dharman Jul 16 '19 at 19:30
  • @marcus It's not my concern, I am checking why that thing is not working if I add the wrong username. If I use any password it gives me an error but on wrong username, it's not giving me an error? why that so? – WebQoda Jul 16 '19 at 19:41
  • Well, I could not find any information related to that on the official PHP docummentation. @Dharman that was just an alternate approach in a easy way, but in a better way, i have provided my own class that I have made a lot of tests and treats generic cases.... – Marcus V. B. Siqueira Jul 16 '19 at 19:44
  • 1
    See https://phpdelusions.net/mysqli/mysqli_connect – Dharman Jul 16 '19 at 19:48