Jump to content

Can connect then doesnt connect !


Recommended Posts

HI there,

I am having a strange issue. I have a php script that says it is connected but when I check the connection object (a few lines of code later), I get error message:

 // Include connect.php which has connect function

 // Get connection Object from connect function
 $conn = connect(); // Prints: Connection is Successful

// Check connection
if( $conn === false){
    die("ERROR: Could not connect. " . mysqli_connect_error()); // Prints: Could not connect. Access denied for user ''@'localhost' (using password: NO)

My code for connect is as follows


function connect()


// Path to Config File

    $config = parse_ini_file('/var/www/vhosts/*******/db.ini');
    $conn = mysqli_connect("localhost",$config['username'],$config['password'],$config['db']);

    // Check connection
    if ($conn->connect_error)
      die("Connection failed: " . $conn->connect_error);
        echo "Connection is successful";

    return $conn;


Its as if the $conn object is NOT being returned properly in the connect() function. This code has worked in other sites.

Any ideas?

Link to comment
Share on other sites

HI there,

I have just found the problem...

I had added in www in to the path to get the ini file. Once I removed the www, it all works fine.

$config = parse_ini_file('/var/www/vhosts/www.*******/db.ini'); // Needed to remove th ewww





Link to comment
Share on other sites

27 minutes ago, jpnsol said:

the above only produces a php warning if $conn is a false value. it also results in a false value for the if() conditional test, which looks like a connection without an error (this is yet another f___ up with the mysqli extension implementation.) it is also due to the different error responses between procedural mysqli and oop mysqli statements.

you should be using exceptions for database statement errors (this is the default setting now in php8+) and in most cases simply do nothing else in your code and let php handle any database exception, where php will use its error related settings to control what happens with the actual error information, via an uncaught exception error (database statement errors will 'automatically' get displayed/logged the same as php errors.) you can then eliminate all the existing error handling logic, since it won't get executed upon an error, simplifying your code.

a) you need to set php's error_reporting to E_ALL and display_errors to ON, preferably in the php.ini on your development system, so that php will help you by reporting and displaying all the errors it detects. stop and start your web server to get any changes made to the php.ini to take effect and use a phpinfo() statement in a .php scrip to confirm that the settings actually took effect.

b) you should be using php8+.

c) to use exceptions for the mysqli extension in older versions of php, add the following line of code before the point where you make the database connection -



Edited by mac_gyver
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.