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?