Home >Database >Mysql Tutorial >Why Does My PHP Code Show an 'Unknown Database Error' While PHPMyAdmin Shows the Database Exists?

Why Does My PHP Code Show an 'Unknown Database Error' While PHPMyAdmin Shows the Database Exists?

Susan Sarandon
Susan SarandonOriginal
2024-12-05 10:50:10551browse

Why Does My PHP Code Show an

Troubleshooting "Unknown Database Error" in PHP with PHPMyAdmin Compatibility

When connecting to MySQL databases using PHP PDO, users may occasionally encounter the "Unknown database error" message, despite the database existing in PHPMyAdmin. This error undermines the user's ability to interact with newly created databases.

To resolve this issue, it's crucial to identify the underlying cause:

  • Spelling Error: Thoroughly check the database name in PHP code and PHPMyAdmin. A simple typo can lead to this error.
  • Database Server Discrepancy: Verify that PHP and PHPMyAdmin are accessing the same database server. This problem arises when multiple database servers are installed on the system.

To determine the server connection details in PHPMyAdmin, execute the query:

show databases;

Compare the results with those obtained from PHP queries using either PDO or mysqli:

$host = 'your db host';
$user = 'your db username';
$pass = 'your db password';

$pdo = new PDO("mysql:host=$host", $user, $pass, [PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION]);
$databases = $pdo->query('show databases')->fetchAll(PDO::FETCH_COLUMN);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect($host, $user, $pass);
$databases = $mysqli->query('show databases')->fetch_all();

By comparing the outputs, you can identify spelling errors or differences in database server connections. In case of differences, check PHPMyAdmin's configuration file to ensure it connects to the appropriate server.

The above is the detailed content of Why Does My PHP Code Show an 'Unknown Database Error' While PHPMyAdmin Shows the Database Exists?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn