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.

im connecting my php script with this simple code:

mysql_connect("localhost", "admin", "1admin") or die("DB Failed"); mysql_select_db("test") or die(mysql_error());

but when execute in terminal, DB failed. however this works on browser.

share|improve this question

closed as off topic by George Stocker, Jay Riggs, marc_s, Gilles, C. A. McCann Jul 12 '11 at 18:55

Questions on Stack Overflow are expected to relate to programming within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here. If this question can be reworded to fit the rules in the help center, please edit the question.

    
What error output is produced? –  George Cummins Jul 12 '11 at 14:47
    
are you really on the same machine? print the error message on mysql_connect() es well, nut just on mysql_select_db and post it here! –  yankee Jul 12 '11 at 14:48
    
hi yankee and george, the errors are : Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock) in /Applications/MAMP/htdocs/project/file.php on line 24 Warning: mysql_connect(): No such file or directory in /Applications/MAMP/htdocs/project/file.php on line 24 No such file or directory –  nuttynibbles Jul 12 '11 at 15:17
    
Please check if there is a difference in mysql.default_socket in your command-line php.ini and your webserver php.ini. –  madflow Jul 12 '11 at 15:42
    
Possible duplicate: stackoverflow.com/questions/4219970/… –  madflow Jul 12 '11 at 15:43

2 Answers 2

Try

mysql_connect(...) or die(mysql_error());

Simply outputting "db failed" is useless for diagnost purposes, especially when MySQL will tell you exactly what's wrong with mysql_error().

share|improve this answer
    
hi the error is No such file or directory –  nuttynibbles Jul 12 '11 at 14:57
    
Is there a db named 'test' in your setup? It should be there, but not necessarily. –  Marc B Jul 12 '11 at 15:01
    
hi my db is not test. i just change the name. but with the correct db, it still output the same error –  nuttynibbles Jul 12 '11 at 15:41
    
Ah. given your comment up above, MySQL's mis-configured and is looking for the server's socket file somewhere where it is (or mysql isn't running). –  Marc B Jul 12 '11 at 15:42

PHP from commandline usually uses a different php.ini from php-cli. If your are on a linux server - look into /etc/php/cli/php.ini (Or something like that...)

Check if mysql is enabled in this php.ini

share|improve this answer
    
im using MAMP on Mac OsX. i checked both php5.2 and php5.3, mysql is enabled –  nuttynibbles Jul 12 '11 at 15:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.