我有一个php脚本,我想从MacOS终端后台运行。然而,我在连接数据库时遇到了问题,但当从浏览器运行完全相同的脚本时没有任何问题。
这是脚本:
<?php
echo "START";
try {
$con = new PDO("mysql:host=localhost;dbname=issues;charset=utf8", "root", "root");
$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $e) {
throw new Exception($e->getMessage());
}
echo "here"; ?>
我正在终端上使用 php script.php
命令运行它。
带有 try catch 块的错误信息如下:
START
致命错误:Uncaught exception 'Exception' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Applications/MAMP/htdocs/issueManager3/notifications.php:11 Stack trace: #0 {main} thrown in /Applications/MAMP/htdocs/issueManager3/notifications.php on line 11
不带 try catch 块的错误信息如下:
START
致命错误:Uncaught exception 'PDOException' with message 'SQLSTATE[HY000] [2002] No such file or directory' in /Applications/MAMP/htdocs/issueManager3/notifications.php:6 Stack trace:#0 /Applications/MAMP/htdocs/issueManager3/notifications.php(6): PDO->__construct('mysql:host=loca...', 'root', 'root')#1 {main} thrown in /Applications/MAMP/htdocs/issueManager3/notifications.php on line 6
它说找不到文件或目录,但它会输出“START”,所以绝对不是找不到脚本的问题。当从终端执行脚本时,是否缺少连接到数据库的内容?
任何帮助都将不胜感激。