mysqli_error() while connecting PHP to MYSQL

2019-09-10 09:41发布

问题:

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

Here are my PHP codes:

<?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.

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

This is line 10 of the code :

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

回答1:

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

With this:

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


回答2:

As about this:

PHP Warning: mysqli_error() expects parameter 1 to be mysqli, boolean given in C:\scripts\neaweather.php on line 10

... it's because you are fetching connection errors incorrectly. When mysqli_connect() fails it doesn't return a resource (what for?) but false so mysqli_error($myConnection) (which expects a resource) fails. There's an example in the PHP manual page with the correct mechanism:

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


回答3:

The default port for MySQL is 3306. In your mysql client you use 3307. Maybe you have to set the port in your php application too.

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


回答4:

Include port in your mysqli_connect since it takes 3306 if not provide

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



标签: php mysql mysqli