PHP: mysql_connect() won't work via command line

后端 未结 7 1829
情书的邮戳
情书的邮戳 2021-01-17 15:37

I have a script which calls mysql_connect() to connect to a MySQL DB. When I run the script in a browser, it works. However, when I run it from a command line

7条回答
  •  别那么骄傲
    2021-01-17 15:51

    This problem can arise when you are running Mac OS X and MAMP. The command line version tries to use the MAC OS X version, which happens to load no php.ini by default.

    You can check that with the command:

    php --ini
    

    See the missing config file?

    Configuration File (php.ini) Path: /etc
    Loaded Configuration File:         !!! THE CONFIG FILE IS MISSING HERE !!!
    Scan for additional .ini files in: /Library/Server/Web/Config/php
    Additional .ini files parsed:      (none)
    

    What you have to do is, load up the php.ini of your choice and copy it to the default location of your command line interpreter which is /etc and then run the above command again, you should see that the php.ini file is loaded which should look like:

    Configuration File (php.ini) Path: /etc
    Loaded Configuration File:         /etc/php.ini
    Scan for additional .ini files in: /Library/Server/Web/Config/php
    Additional .ini files parsed:      (none)
    

    You should also add this to the PHP.ini file in /etc

    mysql.default_socket = /Applications/MAMP/tmp/mysql/mysql.sock
    

    This should fix every problem.

    phew..

提交回复
热议问题