我更喜欢在shell中运行php脚本,因为输出直接输出,而不是像浏览器中的分档一样。这对于看到该脚本实际上仍在工作以及查看长脚本的哪一部分花费最长时间非常有帮助。运行连接到shell中的mysql db的php脚本
当我打电话PHP脚本从shell这样
php filename.php
它工作正常,但只有这样只要没有数据库建立连接的脚本。
显然,mysql_connect()函数似乎失败:
Warning: mysql_connect(): [2002] No such file or directory (trying to connect via unix:///var/mysql/mysql.sock)
我该如何解决这个问题?
谢谢!
查尔斯
编辑1
我连接到数据库如下:
//Connect to db
$user="root";
$databasePassword="";
$host="localhost";
$database="database1";
$identifier=mysql_connect($host,$user,$databasePassword,true);
$db1=mysql_select_db($database);
发现的解决方案
This给我正确的方式做到这一点 - 这是为简单地用“127.0.0.1”替换“localhost”,因为:“原因是”localhost“是一个特定的用于mysql驱动程序的名称使它使用unix套接字连接到mysql而不是一个tcp套接字。“
mysql unix套接字是否存在于/ var/mysql/mysql.sock中? – 2012-07-23 18:49:58
什么操作系统?您可能需要更改mysql的路径,请检查路径 – Kush 2012-07-23 18:49:58
正如[有关mysql_ *'函数的PHP手册一章的介绍(http://www.php.net/manual/en/intro)中所述。 mysql.php):*这个扩展名不推荐用于编写新代码。相反,无论是[mysqli](http://www.php.net/manual/en/book.mysqli.php)还是[PDO_MySQL](http://www.php.net/manual/en/ref.pdo应该使用-mysql.php)扩展名。请参阅[MySQL API概述](http://www.php.net/manual/en/mysqlinfo.api.choosing.php)以获得进一步帮助,以便选择MySQL API。* – eggyal 2012-07-23 18:53:22