简体   繁体   English

PDO MySQL连接问题来自命令行

[英]PDO MySQL Connection Issue from command line

I have a php script I want to run in the background from the terminal on MacOs. 我有一个PHP脚本,我想在MacOs的终端后台运行。 I'm having trouble connecting to the database, however the exact same script has no problems when it's run from the browser. 我无法连接到数据库,但是当从浏览器运行时,完全相同的脚本没有问题。

Heres the script: 继承人脚本:

<?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"; ?>

I'm running it from the terminal with php script.php 我正在使用php script.php从终端运行它

The error with the try catch block I get is: 我得到的try catch块的错误是:

START 开始

Fatal error: 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 致命错误:在/Applications/MAMP/htdocs/issueManager3/notifications.php:11中,消息'SQLSTATE [HY000] [2002]没有这样的文件或目录'的未捕获异常'异常'堆栈跟踪:#0 {main}抛出/第11行的Applications / MAMP / htdocs / issueManager3 / notifications.php

The error I get without the try catch block is: START Fatal error: 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 我在没有try catch块的情况下得到的错误是:START致命错误:在/Applications/MAMP/htdocs/issueManager3/notifications.php中,消息'SQLSTATE [HY000] [2002]没有这样的文件或目录'的未捕获异常'PDOException': 6堆栈跟踪:#0 /Applications/MAMP/htdocs/issueManager3/notifications.php(6):PDO - > __ construct('mysql:host = loca ...','root','root')#1 {main在第6行的/Applications/MAMP/htdocs/issueManager3/notifications.php中抛出

It's saying no such file or directory, but it's echoing out "START" so its definitely not a case of not being able to find the script. 它说没有这样的文件或目录,但是它正在回显“START”,所以它肯定不是无法找到脚本的情况。 Am I missing something I need to add in the connect to a database when the script is being executed from the terminal? 从终端执行脚本时,我是否遗漏了在连接数据库时需要添加的内容?

Any help appreciated. 任何帮助赞赏。

Try this code: 试试这段代码:

   <?php

    echo "START";

    try { 
        $con = new PDO("mysql:host=127.0.0.1;dbname=issues;charset=utf8", "root", "root"); 
        $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
        $con->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
    } 
    catch(PDOException $e) 
{ 
       echo $e->getMessage();
    }

    echo "here"; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM