Jump to content

mysqli_connect suddenly not working


cearlp
Go to solution Solved by mac_gyver,

Recommended Posts

After years of working, the call to mysql_connect does nothing.

Software  installed is:

Server: Localhost via UNIX socket

Server type: MariaDB

Server version: 10.6.18-MAriaDB-0ubuntu0.22.04.1

Protocol version: 10

Apache/2.4.52 (Ubuntu)

Database client version: libmysql - mysqlind 8.1.2-1ubuntu2.18   

PHP extension: mysqli curl mbstring

PHP version: 8.1.2-1ubuntu2.18

What have I missed, some update or could the database be corrupted?

            

 

Link to comment
Share on other sites

Posted (edited)

No error or any message. The program seems to finish normally except nothing was available to be displayed from the database. The code:

if (!($conn=mysqli_connect($host, $user, $psss, &db))) {

   printf("error connecting to DB by user = $user");

   exit;

}

Edited by cearlp
update with more info
Link to comment
Share on other sites

the default setting now in php8+ is to use exceptions for errors for both the mysqli and PDO extensions. assuming you haven't turned this off or caught the mysqli exception yourself, but aren't handling it correctly, a database connection or query error wouldn't allow the code to finish. it would halt at the point of the database error. so, it's more likely that a query just isn't matching any data or is being skipped over. is your code testing if there is not any data from a query and outputting a message stating so?

beyond this, there are just too many possible reasons your code might appear to being displaying nothing from a query. you would need to post all the code necessary to reproduce the problem, less the database connection credentials.

 

Link to comment
Share on other sites

I tried the following after researching mysqli exceptions and the result was the same,,,first two echos are displayed, no error printout but the echo "after conn" was not displayed.

function conn($sql)
{

  $host = "localhost";
  $user = "root";  
  $pass = "xxx";    valid root password for database
  $db   = "xxx";     looking at database with PHPMYADMIN seems okay

echo "in conn--- ";
echo "HOST $host, USER $user, PASS $pass, DB $db";

 $con = new mysqli($host, $user, $pass, $db);
 if ($con->connect_errno) {
    printf("connect failed: %s\n" , $con->connect_error());
    exit();
 }

echo "after conn";

}

Link to comment
Share on other sites

  • Solution

one of the great points of using exceptions for errors is that your main code will only 'see' error free execution. no discrete error checking logic will ever get executed upon an error and should be removed, simplifying the code.

php's error_reporting should always be set to E_ALL. temporarily set display_errors to ON so that php will display all the reported errors, which will now include any uncaught database exceptions.

Link to comment
Share on other sites

Thank you mac_gyver... Finding out the errors showed me that it was a connection to the database that failed due to a password change that was not accounted for in the code.

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.

Guest
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.