我在php代码中提交注释时出错,问题是第4行代码:
php_network_getaddresses:getaddrinfo失败:请求的名称是 有效,但未找到所请求类型的数据。
有什么想法吗?
<?php
if( $_POST )
{
$con = mysql_connect('%', 'myuser', 'mypassword');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("_mysite", $link);
$users_name = $_POST['name'];
$users_email = $_POST['email'];
$users_website = $_POST['website'];
$users_comment = $_POST['comment'];
$users_name = mysql_real_escape_string($users_name);
$users_email = mysql_real_escape_string($users_email);
$users_website = mysql_real_escape_string($users_website);
$users_comment = mysql_real_escape_string($users_comment);
$articleid = $_GET['id'];
if( ! is_numeric($articleid) )
die('invalid article id');
$query = "
INSERT INTO `_mysite`.`comments` (`id`, `name`, `email`, `website`,
`comment`, `timestamp`, `articleid`) VALUES (NULL, '$users_name',
'$users_email', '$users_website', '$users_comment',
CURRENT_TIMESTAMP, '$articleid');";
mysql_query($query);
echo "<h2>Thank you for your Comment!</h2>";
mysql_close($con);
}
?>
答案 0 :(得分:0)
%
是mysql的通配符,当你将它用于你的用户所允许的主机时,它意味着'any host',在你的情况下,它应该是localhost。< / p>
与您的问题无关,但如果您不了解它,则应考虑远离mysql_
deprecated的{{1}}函数,建议替换mysqli。
答案 1 :(得分:0)
将$link
替换为此行$con
中的mysql_select_db("_mysite", $link);
。
这是我第一次看到任何服务器名称为%
。我不太了解它。但是,您可以将%
替换为localhost
或port number
或IP Address
<?php
if($_POST)
{
$con = mysql_connect('localhost', 'myuser', 'mypassword');
if (!$con) {
die('Could not connect: ' . mysql_error());
}
mysql_select_db("_mysite", $con);
$users_name = mysql_real_escape_string($_POST['name']);
$users_email = mysql_real_escape_string($_POST['email']);
$users_website = mysql_real_escape_string($_POST['website']);
$users_comment = mysql_real_escape_string($_POST['comment']);
$articleid = $_GET['id'];
if(!is_numeric($articleid))
die('invalid article id');
$query = "INSERT INTO `_mysite`.`comments` (`id`, `name`, `email`, `website`,`comment`, `timestamp`, `articleid`)
VALUES (NULL, '$users_name','$users_email', '$users_website', '$users_comment',CURRENT_TIMESTAMP, '$articleid')";
mysql_query($query);
echo "<h2>Thank you for your Comment!</h2>";
mysql_close($con);
}
?>
请停止使用mysql*
个功能。这些扩展已在PHP 7中删除。了解PDO
和MySQLi
的准备好的语句。
使用MySqli
<?php
if($_POST)
{
$con = mysqli_connect("localhost", "myuser", "mypassword", "_mysite");
if (!$con) {
die('Could not connect: ' . mysqli_connect_error());
}
$users_name = mysqli_real_escape_string($con, $_POST['name']);
$users_email = mysqli_real_escape_string($con, $_POST['email']);
$users_website = mysqli_real_escape_string($con, $_POST['website']);
$users_comment = mysqli_real_escape_string($con, $_POST['comment']);
$articleid = $_GET['id'];
if(!is_numeric($articleid))
die('invalid article id');
$query = "INSERT INTO `_mysite`.`comments` (`id`, `name`, `email`, `website`,`comment`, `timestamp`, `articleid`)
VALUES (NULL, '$users_name','$users_email', '$users_website', '$users_comment',CURRENT_TIMESTAMP, '$articleid')";
mysqli_query($con,$query)
echo "<h2>Thank you for your Comment!</h2>";
mysqli_close($con);
}
?>