Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have a simple php file I want to execute in the terminal:

script.php

echo "script is running";
$con = mysql_connect("localhost","root","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
else
{
echo "Connection Established\n"; 
}
mysql_close($con);

I'm running the script in the terminal on MacOs with php script.php, and I'm getting the following error:

script is running
Warning: mysql_connect(): No such file or directory in 
/Applications/MAMP/htdocs/issueManager3/notifications.php on line 5
Could not connect: No such file or directory

However, when I run the script through a web browser, it connects to the database fine.

Am I missing something? Or is there something else I need to set up to achieve database connections through the terminal?

Any help appreciated.

share|improve this question
    
Have you checked if they're not loading two different configuration files? On the command line execute php --ini And then create a file to be interpreted by the instance you're serving to the web with the following content: <?php php_info(INFO_GENERAL); ?> –  João Josézinho Sep 6 '14 at 11:22
    
@paperless Yes the configuration file in the terminal is different. How do I change it? –  mickzer Sep 6 '14 at 11:33

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.