1

It's been a while since I posted a question here, but I need some help, trying to understand this behavior.

I have a simple script to debug if my connection was made, using mysqli.

$mysqli = new mysqli(HOST, USER, PASSWORD, DB);

if ($mysqli->connect_error){
    die('Connection error: (' . $mysqli->connect_errno . ') '.$mysqli->connect_error);
}else{
    die(var_dump($mysqli));
}

This works just fine, if the connection wasn't made, for any reason, the connect_error has my error, and script dies, and I know why it died, it tells me. Else, it dies and throws me the connection object properties values, all good.

BUT, if I add a new instance in between the declaration and the debugging, there are 2 possibilities:

Case 1

$mysqli_invalid = new mysqli(HOST, USER, PASSWORD, DB);

$mysqli_valid = new mysqli(HOST_valid, USER_valid, PASSWORD_valid, DB_valid);

if ($mysqli_invalid->connect_error){  <--NOT SET, it comes NULL
    die('Connection error: (' . $mysqli_invalid->connect_errno . ') '.$mysqli_invalid->connect_error);
}else{
    die(var_dump($mysqli_invalid)); <--ALL CLASS PROPERTIES ARE NULL
}

In this case, it jumps to the else, and a mysqli object is thrown, with all its properties NULL.

Case 2

$mysqli_invalid1 = new mysqli(HOST, USER, PASSWORD, DB);

$mysqli_invalid2 = new mysqli(HOST_invalid2, USER_invalid2, PASSWORD_invalid2, DB_invalid2);

if ($mysqli_invalid1->connect_error){  <--SET WITH $mysqli_invalid2 values. 
    die('Connection error: (' . $mysqli_invalid1->connect_errno . ') '.$mysqli_invalid1->connect_error);
}else{
    die(var_dump($mysqli_invalid1));
}

In this case, it dies, but my $mysqli_invalid1, has $mysqli_invalid2 values.

In my project, I have 5 connections, so I need to check if each connection was made right after I try to make the connection. If I try to make all the connections, then check if each was made, it doesn't work like I would expect it to work.

If PHP is object oriented prepared, why does this have this kind of behavior?

Lauro182
  • 1,597
  • 3
  • 15
  • 41
  • If you're doing such complex stuff, I strongly recommend you switch to PDO. It's much nicer. – Barmar Jan 22 '20 at 20:16
  • I just reproduced this, it seems like a bug in mysqli. – Barmar Jan 22 '20 at 20:20
  • It fails in both PHP 5.6 and 7.3. When it shows empty properties, it also prints warnings **var_dump(): Property access is not allowed yet** and **var_dump(): Couldn't fetch mysqli**. – Barmar Jan 22 '20 at 20:25
  • Yeah, I noticed all that. Maybe mysqli bug, like you said. – Lauro182 Jan 22 '20 at 20:28
  • It's very clearly a bug. As you said, each instance should be independent. – Barmar Jan 22 '20 at 20:29
  • And if you just check each connection immediately, you should be fine. Why would you want to delay the checks, anyway? – Barmar Jan 22 '20 at 20:31
  • Looking at the PHP source code, it appears that it is procedural calling for the last connection error, as also stated in the [documentation](https://www.php.net/manual/en/mysqli.connect-error.php) *mysqli::$connect_error -- mysqli_connect_error — Returns a string description of the **last connect error***. But agree it would be expected to be for that specific object. – Will B. Jan 22 '20 at 20:32
  • Yeah, I just do that, I check immediately, but added a new connection copying down and noticed the behavior. Also, if all connections are made properly, it works just fine. I just wanted to know what was happening, as fyrye said, I know connect error returns last connect error, but I would expect my object already filled when the connection fails, when instancing "new mysqli", and each variable holding its own instance. It is just weird behavior, I would think it is on php and not mysqli. – Lauro182 Jan 22 '20 at 20:42
  • 1
    Does this answer your question? [Should we ever check for mysqli\_connect() errors manually?](https://stackoverflow.com/questions/58808332/should-we-ever-check-for-mysqli-connect-errors-manually) – Dharman Jan 22 '20 at 22:10
  • You can't do it really. MySQLi has plenty of bugs and this is one of them. Luckily you do not need to check for the connection errors ever. If you enable mysqli error reporting then you do not need to worry about this bug. – Dharman Jan 22 '20 at 22:14

0 Answers0