我为我的网站创建了一个搜索页面,你可以在这里看到
我从本教程中得到的
在搜索框中输入任何内容并点击搜索时,我收到一些错误消息:
* *警告:mysql_real_escape_string() [function.mysql-real-escape-string]:第 46 行 article.php 中用户 'root'@'localhost' 的访问被拒绝(使用密码:否)
警告:mysql_real_escape_string() [function.mysql-real-escape-string]:无法在第 46 行的 article.php 中建立到服务器的链接
警告:mysql_query() [function.mysql-query]:第 61 行 article.php 中用户 'root'@'localhost' 的访问被拒绝(使用密码:否)
警告:mysql_query() [function.mysql-query]:无法在第 61 行的 article.php 中建立到服务器的链接
警告:mysql_fetch_assoc() 期望参数 1 是资源,在第 64 行的 article.php 中给出的布尔值
谁能明白这意味着什么?
我的 search.php 代码是这样的:
<?php
include_once('include/connection.php');
include_once('include/article.php');
$article = new storearticle();
$articles = $article->fetch_all();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8" />
<title>Search Xclo</title>
<link rel="stylesheet" href="other.css" />
</head>
<body>
<div>
<h1>Search</h1>
<form action="" method="GET">
<p>
<input type="text" name="term" />
<input type="submit" value="search" />
</p>
</form>
</div>
<div>
<?PHP
if (empty($_GET['term']) === false){
$search_results = search_posts($_GET['term']);
if (empty($search_results)){
echo 'Your Search Returned No Results.';
}
foreach ($search_results as $result){
echo "<h3>($result['title'])</h3>";
echo "<p>($result['body'])</p>";
}
}
?>
</div>
</body>
</html>
我的article.php是这样的:
<?php
class storearticle {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT * FROM mobi");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($promo_title) {
global $pdo;
$query = $pdo->prepare("SELECT * FROM mobi WHERE promo_title = ?");
$query->bindValue(1, $promo_title);
$query->execute();
return $query->fetch();
}
}
class category {
public function fetch_all(){
global $pdo;
$query = $pdo->prepare("SELECT DISTINCT `promo_cat` FROM mobi");
$query->execute();
return $query->fetchAll();
}
public function fetch_data($promo_cat) {
global $pdo;
$query = $pdo->prepare("SELECT DISTINCT * FROM mobi WHERE `something` = 'something'");
$query->bindValue(1, $promo_cat);
$query->execute();
return $query->fetch();
}
}
function search_posts($term){
$keywords = preg_split('#\s+#',mysql_real_escape_string($term));
$title_where = "'promo_title' LIKE '%" . implode("%' OR 'promo_title' LIKE '%", $keywords) . "%'";
$content_where = "'promo_content' LIKE '%" . implode("%' OR 'promo_content' LIKE '%", $keywords) . "%'";
$name_where = "'promo_name' LIKE '%" . implode("%' OR 'promo_name' LIKE '%", $keywords) . "%'";
$sql = "SELECT
'promo_title' AS 'title',
LEFT('promo_content', 100) AS 'body',
'promo_name' AS 'name'
FROM 'mobi'
WHERE {$title_where}
OR {$content_where}
OR {$name_where}";
$result = mysql_query($sql);
$results = array();
while (($row = mysql_fetch_Assoc($result)) !== false){
$results[] = $row;
return $results;
}
}
?>
我的 connection.php 代码是这样的:
<?php
try {
$pdo = new PDO('mysql:host=localhost;dbname=xclo', 'xclo', 'xclo');
}catch (PDOException $e){
exit('Database Error.');
}
?>
我可以在此页面中添加什么以使其正常工作?
谢谢。