10

I am looking for a way to test just the connection portion of a php / mysqli connection. I am migrating from a LAMP server build on Vista to the same on Ubuntu and am having fits getting mysqli to work. I know that all of the proper modules are installed, and PhpMyAdmin works flawlessly. I have migrated a site over and none of the mysqli connections are working. The error that I am getting is the "call to member function xxx() on non-object" that usually pops up when either the query itself is bad or the query is prepared from a bad connection. I know that the query itself is good because it works fine on the other server with the exact same database structure and data. That leaves me with the connection. I tried to write a very simple test connection and put it in a loop such as ..

if(***connection here ***) {
    echo "connected";
}
else { 
    echo "not connected"; 
}

It echoes "connected", which is great. But just to check I changed the password in the connection so that I knew it would not be able to connect and it still echoed "connected". So, the if / else test is clearly not the way to go....

3
  • 1
    Can you please show how you do the connection here (you can leave out the user and password)? Commented Sep 9, 2010 at 16:48
  • 1
    "call to member function xxx() on non-object" error has nothing to do with database connection. Commented Sep 9, 2010 at 16:52
  • 1
    Please read: Should we ever check for mysqli_connect() errors manually? Commented Apr 10, 2020 at 11:34

3 Answers 3

18

mysqli_connect() always returns a MySQLi object. To check for connection errors, use:

$mysqli_connection = new MySQLi('localhost', 'user', 'pass', 'db');
if ($mysqli_connection->connect_error) {
   echo "Not connected, error: " . $mysqli_connection->connect_error;
}
else {
   echo "Connected.";
}
Sign up to request clarification or add additional context in comments.

2 Comments

This worked. Thank you, Lek. Of course, it only deepened the mystery. I now know that the database is actually connecting which means that my script is actually spitting the bit at the query itself. This makes even less sense as the query itself works on other servers. I have even written a simple test query that, sadly, still returns the non-object error.
else{ echo "Connected."; } add ;
4

For test php connection in you terminal execute:

$ php -r 'var_dump(mysqli_connect("localhost:/tmp/mysql.sock", "MYSQL_USER", "MYSQL_PASS",
     "DBNAME));'

Comments

-1

You need more error handling on the various database calls, then. Quick/dirty method is to simply do

 $whatever = mysqli_somefunction(...) or die("MySQL error: ". mysqli_error());

All of the functions return boolean FALSE if an error occured, or an appropriate mysqli object with the results. Without the error checking, you'd be doing:

 $result = $mysqli->query("blah blah will cause a syntax error");
 $data = $result->fetchRow();  // $result is "FALSE", not a mysqli_object, hence the "call to member on non-object"

2 Comments

dying i s always bad. At least send 503 status and never reveal any sensitive information like mysql error message. use trigger_error() instead. Although it quite useless here, as PHP error message is already quite informative, if any
Actually I have tried using something similar, only with mysqli_connect_error() It doesn't throw an error.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.