使用mysqli在config.php中选择数据库失败问题求助
Hey Marc, let's work through this database selection issue you're facing—it's a typical hiccup when moving from the old mysql_* functions to mysqli_*, so we can nail this down step by step.
First, let's start with the most likely culprits:
1. You're mixing mysql_* and mysqli_* functions
You mentioned you sometimes have to add an "i" to make things work, which suggests your codebase might be using a mix of both legacy and modern MySQL functions. These two APIs don't play nice together—you can't use a connection created with mysql_connect() to run mysqli_select_db(), and vice versa. Make sure all database-related code is consistently using mysqli_* (since mysql_* is deprecated anyway).
2. Missing or incorrect database name in config
Looking at the config snippet you shared, you have hostname, mysql_user, and mysql_pass, but no entry for the database name itself! You need a line like:
$SETTINGS["mysql_db"] = "your_database_name_here";
Without defining which database you want to select, even a valid connection can't pick one. Double-check that this setting exists and matches the exact name of your MySQL database (case-sensitive on some systems like Linux).
3. Wrong parameter order for mysqli_select_db()
Unlike the old mysql_select_db($dbname, $conn), the mysqli procedural version flips the parameters: it's mysqli_select_db($connection, $dbname). If you're copying the old function's parameter order, that'll cause a failure every time.
Here's how a correct procedural mysqli setup should look:
// Create connection $conn = mysqli_connect($SETTINGS["hostname"], $SETTINGS["mysql_user"], $SETTINGS["mysql_pass"]); // Check connection if (!$conn) { die("Connection failed: " . mysqli_connect_error()); } // Select database if (!mysqli_select_db($conn, $SETTINGS["mysql_db"])) { die("Unable to select database: " . mysqli_error($conn)); }
If you prefer the object-oriented style (cleaner in my opinion), it would be:
$conn = new mysqli($SETTINGS["hostname"], $SETTINGS["mysql_user"], $SETTINGS["mysql_pass"]); if ($conn->connect_error) { die("Connection failed: " . $conn->connect_error); } if (!$conn->select_db($SETTINGS["mysql_db"])) { die("Unable to select database: " . $conn->error); }
4. Permission issues (even though you can connect)
Just because you can log in with root doesn't mean that user has access to your target database. To verify, log into MySQL directly and run:
SHOW GRANTS FOR 'root'@'localhost';
Look for a line like GRANT ALL PRIVILEGES ON your_database_name.* TO 'root'@'localhost';—if it's missing, you'll need to grant access:
GRANT ALL PRIVILEGES ON your_database_name.* TO 'root'@'localhost'; FLUSH PRIVILEGES;
Pro Tip: Always Show Specific Errors
Instead of just outputting "Unable to select database.", use mysqli's built-in error functions to get the exact MySQL error message. This will tell you if the database doesn't exist, you don't have permissions, or there's a typo—way more helpful than a generic message.
Give these steps a try, and let me know if you hit any specific error messages after enabling detailed debugging!
内容的提问来源于stack exchange,提问作者Marc

