当我尝试使用PHP连接mysql并使用php -f filename.php
从命令行执行时,它会抛出“ Warning: mysqli_connect(): (HY000/2002): No such file or directory in ....
”
如果我从浏览器中执行它,它会很好地工作。
$link = mysqli_connect($dbhost, $dbuser, $dbpass) or die("Unable to Connect to '$dbhost'");
mysqli_select_db($link, $dbname) or die("Could not open the db '$dbname'");
$test_query = "SHOW TABLES FROM $dbname";
$result = mysqli_query($link, $test_query);
$tblCnt = 0;
while($tbl = mysqli_fetch_array($result)) {
$tblCnt++;
#echo $tbl[0]."<br />\n";
}
if (!$tblCnt) {
echo "There are no tables<br />\n";
} else {
echo "There are $tblCnt tables<br />\n";
}
谢谢!
答案 0 :(得分:0)
得到答案,我们需要提供UNIX套接字:
mysqli_connect($dbhost, $dbuser, $dbpass, $dbname, NULL, '/Applications/MAMP/tmp/mysql/mysql.sock') or die("Unable to Connect to '$dbhost'");