2016-07-29 73 views
0

我知道有很多與此有關的問題,但我無法找到與我的代碼問題。將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表:

enter image description here

enter image description here

enter image description here

這裏有錯誤,當我跑代碼:

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)); 

回答

0

您的屏幕截圖顯示您的mysql服務器正在端口3307上運行。默認情況下使用3306。 您需要替換此:

$myConnection = mysqli_connect($hostname, $username, $password, $database) or die("Error " . mysqli_error($myConnection)); 

有了這個:

$myConnection = mysqli_connect($hostname, $username, $password, $database, 3307) or die("Error " . mysqli_error($myConnection)); 
+0

@CharlotteDunois否,錯誤HY000/2002通常意味着沒有mysql服務器的存在,他會嘗試連接 –

+0

你是什麼意思沒有MySQL服務器? – plzhelpmi

+0

@plzhelpmi我的意思是沒有mysql服務器在主機和端口上嘗試連接時正在監聽連接 –

0

MySQL的默認端口是3306。在你的MySQL客戶端使用3307也許你需要設置端口在你的PHP應用程序。

myConnection = mysqli_connect($hostname, $username, $password, $database, 3307) 
    or die("Error " . mysqli_error($myConnection)); 
0

包括在你的mysqli_connect端口,因爲它需要3306如果不能提供

mysqli_connect(host,username,password,dbname,port);

0

由於這一點:

PHP的警告:mysqli_error()預計參數1是mysqli的,布爾型 在第10行給出的C:\ scripts \ neaweather.php中

...這是因爲您錯誤地獲取連接錯誤。當mysqli_connect()失敗時,它不會返回資源(用於?),但是false因此mysqli_error($myConnection)(它期望資源)失敗。有一個在PHP manual page與正確的機制的例子:

$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db"); 
if (!$link) { 
    echo "Error: Unable to connect to MySQL." . PHP_EOL; 
    echo "Debugging errno: " . mysqli_connect_errno() . PHP_EOL; 
    echo "Debugging error: " . mysqli_connect_error() . PHP_EOL; 
    exit; 
} 
+0

我不理解我是否很抱歉 – plzhelpmi

+0

好吧...只需刪除'mysqli_error($ myConnection)'並鍵入'mysqli_connect_error( )'。 –