Confusion using MYSQLI

Posted by user1020069 on Stack Overflow See other posts from Stack Overflow or by user1020069
Published on 2012-06-25T20:12:15Z Indexed on 2012/06/25 21:16 UTC
Read the original article Hit count: 179

Filed under:
|
|

I just started using mysqli API for PHP. Apparently, every time an object of the class MYSQLI is instantiated, it can setup a connection to the database as it connects to the server unlike mysql_connect, which connects to the server first and then you are required to specify the database to query.

Now this is a good problem if the db exists, in my case, the db does not exist on the first ever connection to the server/execution of the problem, hence I must connect without specifying the database, which is fine, since the msyqli constructor does not make this database mandatory.

My challenge is essentially, how do I check if the database exists before attempting the first connection. The only way to really do this would be to establish a conection to the server and then use the result of the following query to gauge if the database exists:

SELECT COUNT(*) AS `exists` FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMATA.SCHEMA_NAME="dbname" ;

If this returns true, then the database exists, but now the challenge is how do I get the mysqli object to query this database rather than having to prefix the name of the database in the query.

Thanks much

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql