我知道有很多與此有關的問題,但我無法找到與我的代碼問題。將PHP連接到MYSQL時mysqli_error()
這裏是我的PHP代碼:
<?php
//set the connection variables
$hostname = "localhost";
$username = "root";
$password = "password";
$database = "fyp";
$filename = "C:/scripts/nea.csv";
//connect to mysql database
$myConnection = mysqli_connect($hostname, $username, $password, $database)
or die("Error " . mysqli_error($myConnection));
// open the csv file
$fp = fopen($filename,"r");
//parse the csv file row by row
while(($row = fgetcsv($fp,"500",",")) != FALSE)
{
//insert csv data into mysql table
$sql = "INSERT INTO neaweather (ValidTime, Date, Time, Lat, Lon, AreaName) VALUES ('".mysql_escape_string($row[0])."','".mysql_escape_string($row[1])."','".mysql_escape_string($row[2])."','".mysql_escape_string($row[3])."','".mysql_escape_string($row[4])."','".mysql_escape_string($row[5])."')";
$query = mysqli_query($myConnection,$sql);
if($query){
echo "row inserted\n";
}
else{
echo die(mysql_error());
}
}
fclose($fp);
//close the db connection
mysqli_close($myConnection);
?>
的代碼工作完全正常,當我昨天跑了。
下面是詳細到MySQL表:
這裏有錯誤,當我跑代碼:
C:\scripts>php neaweather.php
PHP Warning: mysqli_connect(): (HY000/2002): No connection could be made
because the target machine actively refused it.in C:\scripts\neaweather.php
on line 10
Warning: mysqli_connect(): (HY000/2002): No connection could be made because
the target machine actively refused it. in C:\scripts\neaweather.php on line
10
PHP Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given
in C:\scripts\neaweather.php on line 10
Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in
C:\scripts\neaweather.php on line 10
Error
這是第10行代碼:
//connect to mysql database
$myConnection = mysqli_connect($hostname, $username, $password, $database)
or die("Error " . mysqli_error($myConnection));
@CharlotteDunois否,錯誤HY000/2002通常意味着沒有mysql服務器的存在,他會嘗試連接 –
你是什麼意思沒有MySQL服務器? – plzhelpmi
@plzhelpmi我的意思是沒有mysql服務器在主機和端口上嘗試連接時正在監聽連接 –