使用PHP7.1.7+XAMMP连接MySQL数据库遇1049错误求助
Hey there, let's troubleshoot that MySQL 1049 error you're hitting with your XAMPP + PHP 7.1.7 setup! Error 1049 essentially means MySQL can't find the database name you're specifying—even if you see shop in phpMyAdmin, there are a few easy-to-miss details that might be causing this. Here's how to fix it:
1. Double-check the database name (spelling & case matter!)
- MySQL is case-sensitive on Linux/macOS by default, while Windows ignores case. First, confirm the database name in phpMyAdmin is exactly
shop—no accidental spaces, underscores, or capitalization (likeShoporSHOP). - Look closely at the database name string in your code: make sure
'shop'has no invisible spaces or special characters (these can sneak in when copying/pasting text).
2. Verify MySQL service status & connection parameters
- Open your XAMPP control panel and make sure the MySQL service is marked Running. If it's stopped, start it up first—you can't connect to a service that isn't running!
- Your code uses
127.0.0.1,root, and an empty password (XAMPP's default config), but try swapping127.0.0.1withlocalhost—sometimes these use different connection methods (TCP vs. local socket) and one might work when the other doesn't. - If you ever changed the root password for MySQL, update the empty string in your code to match your new password.
3. Confirm the shop database exists in your MySQL instance
- In phpMyAdmin, check the left sidebar's database list—make sure
shopis listed there. - You can also run a quick SQL query in phpMyAdmin's console to verify:
IfSHOW DATABASES;shopdoesn't appear in the results, you might have created it in the wrong instance, or the creation failed silently. Try re-creating the database in phpMyAdmin.
4. Test with a simplified connection script
Rule out any other code in functions.php causing issues by using a bare-bones test script:
<?php $host = '127.0.0.1'; $user = 'root'; $password = ''; $dbname = 'shop'; $conn = new mysqli($host, $user, $password, $dbname); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } echo "Database connected successfully!"; ?>
If this script still throws 1049, the problem is definitely with the database name or connection parameters. If it works, then your original functions.php has some hidden issue to debug.
5. Check root user permissions (just to be safe)
While the root user usually has full access, it's worth confirming it has permissions for the shop database:
- In phpMyAdmin, go to the "User accounts" tab, find the
rootuser, and check its permissions. Make sure it has full access (or at least basic read/write permissions) for theshopdatabase.
Most 1049 errors boil down to simple typos, case mismatches, or a stopped MySQL service. Work through these steps and you'll get that connection working in no time!
内容的提问来源于stack exchange,提问作者weng tee

