我更喜歡在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