4

I have a php script I want to run in the background from the terminal on 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

The error with the try catch block I get is:

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

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

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. 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.

  • @jeroen Check the edit above – user3763741 Sep 06 '14 at 01:52
  • @jeroen Using 127.0.0.1 gives the same error – user3763741 Sep 06 '14 at 01:59
  • Are you aware that when you run things from CRON you generally need absolute paths? Things that easily work with apache need absolute paths if they need to be run via cron. Make sure your cron run can find all the files required. See all the includes and requires – Hanky Panky Sep 06 '14 at 02:26
  • @Hanky웃Panky Running it with the absolute path to the script doesn't work either... And as you can see there are no includes or requires in the script. – user3763741 Sep 06 '14 at 10:33
  • Did you go through this ? http://stackoverflow.com/questions/2412009/starting-with-zend-tutorial-zend-db-adapter-throws-exception-sqlstatehy000 and this? http://stackoverflow.com/questions/20723803/pdoexception-sqlstatehy000-2002-no-such-file-or-directory – Hanky Panky Sep 06 '14 at 16:24

1 Answers1

0

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"; ?>
yuvraj d
  • 27
  • 3