jpnsol Posted September 13, 2023 Share Posted September 13, 2023 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 require('connect.php'); // 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 <?php 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); } else { 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? Quote Link to comment Share on other sites More sharing options...
requinix Posted September 13, 2023 Share Posted September 13, 2023 Have you checked your PHP error log for messages? Perhaps a message occurring on if ($conn->connect_error) this line? Quote Link to comment Share on other sites More sharing options...
jpnsol Posted September 13, 2023 Author Share Posted September 13, 2023 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 Thanks, John Quote Link to comment Share on other sites More sharing options...
mac_gyver Posted September 13, 2023 Share Posted September 13, 2023 (edited) 27 minutes ago, jpnsol said: $conn->connect_error 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 - mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT); Edited September 13, 2023 by mac_gyver Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.