-2
<?php
/* die/exit  operation*/
mysqli_connect('localhost','root','') or die ('The connection is lost');
echo 'connected';
?>

mysqli_connect or die functions working together fine in case of the both correct and incorrect host names.But no matter what username I am using,it is always showing 'connected'.Can anyone please tell me why it is happening?

  • Can you please share what you are trying to achieve, so i can help? – Joseph Daudi Dec 29 '18 at 06:44
  • 1
    It's showing connected because your echo 'connected'; is not in an if statement that checks if the user is connected or not. show us more of your code and we can help with that. – Patrick Simard Dec 29 '18 at 06:53
  • I am just trying to test the (mysqli_connect or die) operation.Name of my host is 'localhost' and username is 'root'.In case of host name, the echo or die is executed perfectly.But whenever I am using username other than root,it is still showing the echo result and die function is not excecuting. – Azizul Hakim Chowdhury Dec 29 '18 at 06:55
  • Can you add `error_reporting(E_ALL);` and `ini_set('display_errors', 1);` to the start of the script and see if it reports anything. – Nigel Ren Dec 29 '18 at 07:21

1 Answers1

0

I don't think your die statement will ever be reached. mysql_connect is the alias for mysqli::connect and the object will be made.

$mysqli = new mysqli('localhost', 'my_user', 'my_password', 'my_db');

To get a connection failure:

if ($mysqli->connect_error) {
    die('Connect Error (' . $mysqli->connect_errno . ') ' . $mysqli->connect_error);
}
Henry
  • 1,242
  • 1
  • 12
  • 10