MySQLi连接时没有指定数据库

时间:2015-02-16 15:19:43

标签: php database pdo mysqli

我正在使用许多数据库,我想知道是否可以连接mysqli并在以下情况下指定数据库:example

$mysqli = new mysqli("localhost", "user", "password", "database");
$req = $mysqli->query("SELECT * FROM `table` WHERE id = '1' ");

会变成

$mysqli = new mysqli("localhost", "user", "password");
$req = $mysqli->query("SELECT * FROM `database`.`table` WHERE id = '1' ");

我可以添加$mysqli->select_db("database");但是如果有办法使用上面的查询,我想知道该怎么做。谢谢!

1 个答案:

答案 0 :(得分:0)

是的,这个方法其实非常简单, 只是做

$mysqli = new mysqli("localhost", "user", "password");
$res = $mysqli->query("SHOW DATABASES");

while ($row = $res->fetch_assoc()) {
    echo ($results, $row['Database'] . "\n");
}