9

I'm trying to create a connection to a MySQL database using Mysqli in PHP. When I execute the following code on a stand alone page:

<?php
ini_set('display_errors', 'On');
error_reporting(E_ALL);

$link = new mysqli('localhost', 'myuser', 'mypass', 'dbname');
var_dump($link);

All I get outputted is an empty Mysqli object where all the properties are null. No error or anything gets displayed.

I also don't see any entries in the Apache or MySQL logs. I'm kind of at a lost on this one.

hakre
  • 193,403
  • 52
  • 435
  • 836
Justin Chmura
  • 1,939
  • 1
  • 15
  • 17
  • try `$link = new mysqli('localhost', 'myuser', 'mypass', 'dbname') or die(mysqli_error());` – crush Feb 15 '12 at 22:18
  • 1
    @crush: I think you mean `$link = new mysqli('localhost', 'myuser', 'mypass', 'dbname') or die(mysqli_connect_error());`. The `mysqli_error()` function expects a link identifier as its parameter. – Justin ᚅᚔᚈᚄᚒᚔ Feb 15 '12 at 22:24
  • @Justin, I changed it to what you recommended and it didn't change at all. After instantiatingthe mysqli object, the mysql_connect_error function returns null, assuming no error. Though the mysqli object's properties are all null. When I dump the $link, I get: – Justin Chmura Feb 15 '12 at 22:37
  • Try running your stand-alone page via the command-line PHP interpreter and revise your question with the output. – Justin ᚅᚔᚈᚄᚒᚔ Feb 15 '12 at 22:42
  • Sorry, I thought I pasted in the output. Here's what the var_dump is: http://pastie.org/3390699 – Justin Chmura Feb 15 '12 at 22:48
  • Ok, the output I pasted above is from the browser, running it from the command line yields a fully populated mysqli object. Why would it work from the command line and not the browser? – Justin Chmura Feb 15 '12 at 22:50
  • Please list your operating system and version numbers for MySQL, PHP and Apache. This sounds like a very specific issue with your configuration. – Justin ᚅᚔᚈᚄᚒᚔ Feb 16 '12 at 16:25

2 Answers2

9

I had this problem too and was going crazy trying to debug it. It turns out that sometimes for whatever reason the mysqli object does not get populated, but directly accessing it's properties still executes the native code behind it. So even though a var_dump of the entire mysqli object shows null properties, they are there if you access them individually. If errorno turns out to be false, you may have executed a valid query with an empty resultset that you weren't expecting. Hope this helps.

$mysqli = mysqli_connect('localhost', 'root', '', 'test', 3306);

var_dump($mysqli);

var_dump($mysqli->client_info);
var_dump($mysqli->client_version);
var_dump($mysqli->info);

and the output:

object(mysqli)[1]
  public 'affected_rows' => null
  public 'client_info' => null
  public 'client_version' => null
  public 'connect_errno' => null
  public 'connect_error' => null
  public 'errno' => null
  public 'error' => null
  public 'field_count' => null
  public 'host_info' => null
  public 'info' => null


public 'insert_id' => null
  public 'server_info' => null
  public 'server_version' => null
  public 'stat' => null
  public 'sqlstate' => null
  public 'protocol_version' => null
  public 'thread_id' => null
  public 'warning_count' => null

string 'mysqlnd 5.0.8-dev - 20102224 - $Revision: 321634 $' (length=50)
int 50008
null
int 0
string 'localhost via TCP/IP' (length=20)
string '5.5.20-log' (length=10)
int 50520
danperron
  • 106
  • 1
  • 3
  • Just ran across this problem as well and your solution worked, but I did find `print_r` will appropriately print out the object as well. – user1669496 Jun 06 '14 at 13:52
3

I know this is a bit old, but for anyone who still has this as a recurring issue, the comment from user3158900 to danperron's post has the solution. I'm re-posting here to make it more visible. Also, some example code is included:

global $mysqli; //assuming you've already created your $mysqli object
$mysqli->query("INSERT INTO users SET username='jim' and password='somehash'");
var_dump( $mysqli ); //prints the full object but all the values are null 

//the following code prints the full object with appropriate values such as
//insert_id, affected_rows, etc
//view http://pastebin.com/Mgd2CZsM for an example of this output
echo "<pre>" . print_r( $mysqli, true ). "</pre>"; 
Elly Post
  • 336
  • 3
  • 12