-1

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.

chrizonline
  • 4,779
  • 17
  • 62
  • 102
  • 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 – chrizonline 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: http://stackoverflow.com/questions/4219970/warning-mysql-connect-2002-no-such-file-or-directory-trying-to-connect-via – madflow Jul 12 '11 at 15:43

2 Answers2

3

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

madflow
  • 7,718
  • 3
  • 39
  • 54
0

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

Marc B
  • 356,200
  • 43
  • 426
  • 500