Skip to content

mysqli_select_db still throws error even with no error reporting – similar to #8772 #18558

Closed
@SoulThy

Description

@SoulThy

Description

Reopening the issue:

Hi, I found this issue while troubleshooting a similar problem such as the one referred in #8772 in my own project.
I saw that the original author experienced errors from mysqli_select_db() when the database did not exist, and solved it by removing MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT.

However, in my case, removing all error reporting did not fix the issue. The same behavior persists — mysqli_select_db() still throws a fatal error instead of returning false, and it stops my script.

The code presents a series of echos for debugging purposes.

My code

database_init.php

<?php

// ini_set('display_errors', 1);
// error_reporting(1);
// mysqli_report(MYSQLI_REPORT_ERROR);

require_once 'database_connection.php';

$queryCreateDB = "CREATE DATABASE $database";

$r = 0;

echo "Im about to select the database: " . $database . "<br/>";
$r = mysqli_select_db($conn,$database);
echo "I just run mysqli_select_db<br/>";

echo "r->" . gettype($r) . "<br/>";
echo "r->" . $r . "<br/>";

if(!$r){
    echo "Did not find database! Creating one!<br/>";

    if($conn->query($queryCreateDB)){
        echo "Finished query! <br/>";
    }
    else{
        die("Database creation failed: " . $conn->connect_error);
    }
}
else{
    echo "A database named " . $database . "has been found!<br/>";
}

$conn->close();
?>

Screenshot from my browser

Image

Looking at the screenshot we can see how the code will terminate right here:

$r = mysqli_select_db($conn,$database);

Extra information

Uncommenting the line:

ini_set('display_errors', 1);

Will show me the following error:

Image

I'm here to provide any additional information. Thanks.

PHP Version

PHP 8.3.6 (cli) (built: Mar 19 2025 10:08:38) (NTS)
Copyright (c) The PHP Group
Zend Engine v4.3.6, Copyright (c) Zend Technologies
    with Zend OPcache v8.3.6, Copyright (c), by Zend Technologies

Operating System

Ubuntu 24.04.2 LTS

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions