2

I have a very simple piece of code.

$mysqli = new mysqli($host, 'user', 'pass', $dbname);  
        if ($mysqli->connect_error) {
           //       ...
        }
 print_r($mysqli);
//  ...

There are 2 servers. One with PHP 5.3 another with 5.2.4 First server works without problems, and print all details about the connection.

The second server doesn't return any error. So it looks like connection succeed. But the object is empty (while exists)

mysqli Object
(
)

What can be the problem?

Kermit
  • 33,827
  • 13
  • 85
  • 121
Alexander P
  • 775
  • 1
  • 9
  • 19
  • I see this type of question all the time, you should read [common database debugging for PHP and MySQL](http://jason.pureconcepts.net/2013/04/common-debugging-php-mysql/). – Jason McCreary Aug 22 '13 at 15:47
  • Thank you for you advise. As I mentioned completely the same script works with the same database. So all your basic checklist is OK – Alexander P Aug 23 '13 at 06:56

2 Answers2

1

The answer is found at New Mysqli Object is Null

Some more tests showed that though the object seems to be empty the properties are not. And can be accesed directly. So if I do

print_r($mysqli->host_info);

I get an expected data. While

print_r($mysqli)

shows as empty. It looks like a bug in PHP 5.2.x As I mentioned in the question it works normally on the server with PHP 5.3

Community
  • 1
  • 1
Alexander P
  • 775
  • 1
  • 9
  • 19
-1
$mysqli = new mysqli($host, 'user', 'pass', $dbname);  
if (mysqli_connect_errno()) {
    throw new Exception(mysqli_connect_errno()." ".mysqli_connect_error());
}

will work for any version

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • No, don't mix OO and procedural. Or are you suggesting that, in PHP 5.2, we _have to_ for this property? I can't see any evidence of that in [the manual's page on this topic](http://www.php.net/manual/en/mysqli.connect-errno.php). Please be clear and explain your answer. – Lightness Races in Orbit Aug 22 '13 at 17:02
  • No. Also this doesn't. if (mysqli_connect_errno()) { throw new Exception(mysqli_connect_errno()." ".mysqli_connect_error()); echo 'Error'; } else { echo 'No error'; } print_r($mysqli); Returns No error mysqli Object ( ) – Alexander P Aug 23 '13 at 07:17
  • @AlexanderPresman ok, I suppose that `$mysqli = mysqli_init();` returns empty object too? Are you sure you can see all the PHP errors? – Your Common Sense Aug 23 '13 at 07:46
  • I suppose this answer was posted before you learned that you can force warnings into exceptions? :) I am trying to find an answer to my question when can `mysqli_connect_error` be useful and I found this answer. – Dharman Nov 02 '19 at 17:09
  • @Dharman yes, it was a week before [I learned](https://stackoverflow.com/q/18457821/285587) about mysqli_report. The purpose of this function is the consistent API. As long as there is a procedural API and two ways to check for errors, this function must be there. – Your Common Sense Nov 02 '19 at 17:13
  • Another things which is pointlessly abused I suppose. If mysqli can throw warnings/exceptions on its own then checking manually for errors is just pointless. – Dharman Nov 02 '19 at 17:15
  • 1
    @Dharman I wouldn't call it this way. In a hindsight, it is rather pointless now. But a backward compatibility is important. This function has been added at the time when nobody had a idea of using exceptions but everyone checked the function's result manually. – Your Common Sense Nov 02 '19 at 17:38