2002:从 PHP My SQL 插入数据时连接超时

我正在编写这个 PHP 程序,它将一条记录插入一个名为事件的表中。PHP代码是:


<?php

$servername = "example.com";

$username = "dev";

$password = "some password";

$dbname = "mydb";


$dateModified = gmdate("Y-m-d h:i:s a");


// Create connection

$conn = new mysqli($servername, $username, $password, $dbname);


// Check connection

if ($conn->connect_error) {

    die($conn -> connect_errno. " : ".$conn -> connect_error);

}


$sql = "INSERT INTO events (ID, eventName, timeStamp, dateModified)

VALUES ('1', 'Login', 'Login', '12/12/2019',".$dateModified;


if ($conn->query($sql) === TRUE) {

    echo "New record created successfully";

} else {

    echo "Error: " . $sql . "<br>" . $conn->error;

}


$conn->close();


?>

我收到此错误:


2002 : Connection timed out

有趣的是,如果我将它包装在一个函数中并尝试相同的方法,则会出现此错误:


1045 : Access denied for user ''@'localhost' (using password: YES)

我哪里错了?


撒科打诨
浏览 86回答 1
1回答

精慕HU

查询中有语法错误。最后缺少封闭括号字符串登录被给出了两次请参阅下面的更正版本。$sql&nbsp;=&nbsp;"INSERT&nbsp;INTO&nbsp;events&nbsp;(`ID`,&nbsp;`eventName`,&nbsp;`timeStamp`,&nbsp;`dateModified`) VALUES&nbsp;('1',&nbsp;'Login',&nbsp;'12/12/2019',&nbsp;'$dateModified')";拒绝访问意味着,用户dev没有插入权限。
打开App,查看更多内容
随时随地看视频慕课网APP