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
Release: 2024-12-05 10:50:10
Original
548 people have browsed it

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;
Copy after login

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);
Copy after login
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = mysqli_connect($host, $user, $pass);
$databases = $mysqli->query('show databases')->fetch_all();
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template