问题描述
我收到此错误
请注意地址中的双"端口: localhost:3306:3306
Notice the "double" port in the address: localhost:3306:3306
xxDb.php line
32看起来像这样:
xxDb.php line
32 looks like this:
$db = new PDO('mysql:host='.DB_HOST.';dbname='.DB_NAME.';charset=utf8', DB_USER, DB_PW, array( PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8" ));
转储DB_HOST会导致localhost:3306
.
dumping DB_HOST results in localhost:3306
.
在连接初始化期间,我看不到第二个端口部分3306
明显来自何处.任何帮助都将受到高度赞赏.
I can not see where the second port part 3306
comes from which obviously exists during the connection initialization.Any help is highly appreciated.
推荐答案
showdev的评论正确,即PDO DSN不允许使用host:port语法.
showdev's comment is correct that the PDO DSN does not allow host:port syntax.
如果CMS在控件之外定义DB_HOST,则不能直接使用该常量.但是您可以从中提取信息.
If your CMS is defining DB_HOST outside of your control, you can't use that constant directly. But you can pull information out of it.
$host_port = preg_replace('/:(\d+)/', ';port=${1}', DB_HOST);
$db = new PDO("mysql:host={$host_port};dbname=".DB_NAME.";charset=utf8",
DB_USER, DB_PW, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
这篇关于由于“双端口",PHP PDO初始化失败. -未捕获的PDOException:SQLSTATE [HY000] [2002]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!