我知道有很多与此相关的问题,但我无法用我的代码找到问题。
以下是我的PHP代码:
$model = new LoginForm();
if(isset(Yii::$app->user->isGuest)){
return $this->render('login', [
'model' => $model,//Model is the user active record
]);//render the view file located in **site folder** login.php **view file**
}
$model = Users::findOne(yii::$app->user->id);//model is the users model used to display controls in index.php view
return $this->render('/site/index', ['model' => $model]);
昨天我运行时代码完全正常。
以下是MYSQL表的详细信息:
运行代码时出现以下错误:
<?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);
?>
这是代码的第10行:
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
答案 0 :(得分: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));
答案 1 :(得分:0)
MySQL的默认端口是3306.在你的mysql客户端中你使用3307.也许你必须在php应用程序中设置端口。
myConnection = mysqli_connect($hostname, $username, $password, $database, 3307)
or die("Error " . mysqli_error($myConnection));
答案 2 :(得分:0)
在mysqli_connect中包含端口,因为如果不提供则需要3306
mysqli_connect(host,username,password,dbname,port);
答案 3 :(得分:0)
关于这一点:
PHP警告:mysqli_error()期望参数1为mysqli,boolean 在第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;
}