mysqli_connect连接到什么数据库表?

时间:2014-03-18 16:10:38

标签: php mysql

我正在阅读一篇教程,声明连接到MySQL数据库的语法如下:

$connection = mysqli_connect("hostname", "username", "password");

令我困惑的是,您可以拥有多个数据库,但实际上您并未提供数据库名称。这是否意味着它实际连接到一组数据库,或者我错过了一个步骤,我必须以某种方式指定我需要连接到的数据库?

6 个答案:

答案 0 :(得分:3)

第四个参数是默认数据库名称source,您可以使用mysqli_select_db更改数据库。

答案 1 :(得分:2)

mysqli_connect函数实际上可以使用4个参数。最后一个是数据库名称。

$connection = mysqli_connect("hostname", "username", "password", "db_name");

这就是你所需要的。

编辑:根据评论改写我的答案。谢谢@TimWolla

答案 2 :(得分:0)

使用mysqli_connect表示您正在连接数据库。

您的格式可以是这样的:

mysqli_connect("myhost","myuser","mypassw","mybd")

答案 3 :(得分:0)

它连接到MySQL守护进程,就像mysql命令行实用程序一样。您可以访问所选用户可以访问的每个数据库:

[timwolla@~]mysql -u root
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 665
Server version: 5.5.35-0ubuntu0.13.10.2 (Ubuntu)

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> 

您可以在之后更改数据库(select_db

mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed

或者在您执行的每个查询中指定正确的一个,方法是在表格前加上正确的数据库:

mysql> SELECT * FROM test.a;
Empty set (0.00 sec)

您甚至可以一次从多个数据库中进行选择:

mysql> SELECT * FROM test.b UNION SELECT * FROM md5.hashes LIMIT 5;
+------------+----------------------------------+
| primary    | a                                |
+------------+----------------------------------+
| 1693571230 | 520b970bb67846dbc9392ca8d487d92c |
| 702707365  | 0e86d6992bc75c2f3fb679cdf321252c |
| 2133213448 | 949c4d575ff4804de7e2a17c42bce2f1 |
| 280504773  | 3662f03fa083807e8407aa662de1aa73 |
| 130287311  | 087374e9f6f64b879705c853224ab67d |
+------------+----------------------------------+
5 rows in set (0.05 sec)

答案 4 :(得分:0)

您还可以使用以下方式切换数据库:

mysqli_select_db()

例如:

mysqli_select_db($connection, "db_name");

答案 5 :(得分:0)

它连接到MySQL服务,然后返回资源。此时,您没有连接到特定数据库,而是连接到存储在变量$ connection中的服务本身。

然后,您可以使用mysqli_select_db()选择要查询的数据库。

最后一个参数是您要连接的默认数据库,这是可选的。

通过提供最后一个参数,您可以节省额外的一步。

相关问题