gpt4 book ai didi

php - 用户 'root' @'localhost' 的访问被拒绝(使用密码 : NO)

转载 作者:行者123 更新时间:2023-11-29 06:19:59 26 4
gpt4 key购买 nike

我正在使用一个简单的表单来进行数据库查询。通过我在代码中包含的密码访问数据库。我不确定为什么我总是在字符串转义和 undefined variable 上遇到错误 $query = htmlspecialchars($query);

<?php
$servername = "localhost";
$username = "xxx";
$password = "xxx";
$dbname = "xxx";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Search</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="style.css"/>
</head>
<body>
<form action="Search2.php" method="POST">
<input type="text" name="query" />
<input type="submit" value="Search" />
</form>

<?php

if (isset($_POST['query']))
$query = $_POST['query'];

if (!empty($query))

$query = $_POST['query'];
// gets value sent over search form


$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to &gt;

$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection

$raw_results = mysql_query("SELECT LastName, FirstName FROM Staff
WHERE (`LastName` LIKE '%".$query."%') OR (`FirstName` LIKE '%".$query."%')") or die(mysql_error());


if(mysql_num_rows($raw_results) > 0){ // if one or more rows are returned do following

while($results = mysql_fetch_array($raw_results)){
// $results = mysql_fetch_array($raw_results) puts data from database into array, while it's valid it does the loop

echo "<p><h3>".$results['LastName']."</h3>".$results['FirstName']."</p>";
// posts results gotten from database(title and text) you can also show id ($results['id'])
}

}
else{ // if there is no matching rows do following
echo "No results";
}


?>



</body>
</html>

最佳答案

这里的问题是您已经使用您的凭据调用了一个 mysqli 对象,但是,稍后您尝试使用 mysql_ 过程方法执行。你在那里没有连接。您需要坚持使用 mysqli 对象。此外,您应该使用准备好的语句来处理用户对 SQL 查询的输入。

删除这些,我们不需要对准备好的语句进行清理:

//BYE!
$query = htmlspecialchars($query);
// changes characters used in html to their equivalents, for example: < to &gt;

$query = mysql_real_escape_string($query);
// makes sure nobody uses SQL injection

现在让我们使用 mysqli 对象和 OOP 准备的方法。然而,首先我们需要构建我们的 like 语句,因为我们的查询变量没有被执行,你不能将 %?% 直接连接到 prepared() 语句中。

$query = '%'.$query.'%';

$stmt = $mysqli->prepare("SELECT LastName, FirstName FROM Staff
WHERE LastName LIKE ? OR FirstName LIKE ?");

现在我们可以将参数绑定(bind)到我们的 $stmt 对象。

$stmt->bind_param('ss', $query, $query);

让我们现在执行它并取回我们的数据。

$result = $stmt->execute();

然后我们可以循环:

while ($row = $result->fetch_assoc()) {
echo "<p><h3>".$result['LastName']."</h3>".$result['FirstName']."</p>";
}

编辑

您也不需要使用反引号转义您的列名,因为:

  • 他们的名字中没有-
  • 它们不是 MySQL 中的保留特殊字。

关于php - 用户 'root' @'localhost' 的访问被拒绝(使用密码 : NO),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33833284/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com