本文介绍了将PHP连接到MYSQL时发生mysqli_error()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道与此相关的问题很多,但是我的代码找不到问题.

I know that there are tons of questions related to this but I can't find the problem with my codes.

这是我的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);

?>

我昨天运行它时,代码运行得很好.

The codes worked perfectly fine when I ran it yesterday.

这是MYSQL表的详细信息:

Here are the details to MYSQL table :

这是我运行代码时的错误:

Here are the error when I ran the codes :

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

推荐答案

您的屏幕截图显示您的mysql服务器在端口3307上运行.默认情况下使用3306.您需要替换此:

Your screenshot says your mysql server is running on port 3307. By default 3306 is used.You need to replace this :

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

这篇关于将PHP连接到MYSQL时发生mysqli_error()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 08:27