gpt4 book ai didi

php - 如何通过 mysqli 连接到不同的 MySQL 端口?

转载 作者:行者123 更新时间:2023-12-03 19:39:05 25 4
gpt4 key购买 nike

在 PHP 的 mysqli doc 中, 它说

mysqli mysqli_connect ([ string $host = ini_get("mysqli.default_host") [, string $username = ini_get("mysqli.default_user") [, string $passwd = ini_get("mysqli.default_pw") [, string $dbname = "" [, int $port = ini_get("mysqli.default_port") [, string $socket = ini_get("mysqli.default_socket") ]]]]]] )



但是当我这样做的时候
$dblink = new \mysqli($c['host'], $c['user'], $c['pass'], $c['name'], $c['port']);

它连接到默认端口。
只有在我这样做之后
$dblink = new \mysqli($c['host'].":".$c['port'], $c['user'], $c['pass'], $c['name'], $c['port']);

它是否连接到正确的端口。

最佳答案

mysqli_connect() 的文档提到:

host

Can be either a host name or an IP address. Passing the NULL value or the string "localhost" to this parameter, the local host is assumed. When possible, pipes will be used instead of the TCP/IP protocol.



这是底层 libmysql的行为图书馆。在已弃用的 mysql_connect() 的文档中也提到了这一点。函数 where 解决方案 还提供:

Note:

Whenever you specify "localhost" or "localhost:port" as server, the MySQL client library will override this and try to connect to a local socket (named pipe on Windows). If you want to use TCP/IP, use "127.0.0.1" instead of "localhost".



该行为也在 PDO_MYSQL DSN 的文档中指定。但这次更加晦涩:

Note: Unix only:

When the host name is set to "localhost", then the connection to the server is made thru a domain socket. If PDO_MYSQL is compiled against libmysqlclient then the location of the socket file is at libmysqlclient's compiled in location. If PDO_MYSQL is compiled against mysqlnd a default socket can be set thru the pdo_mysql.default_socket setting.

关于php - 如何通过 mysqli 连接到不同的 MySQL 端口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42957714/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com