search.php页面返回错误
我正在为我的网站创建一个搜索页面 这是我从 在搜索框中输入任何内容并点击搜索时,我会收到一些错误信息,如: **警告:mysql\u real\u escape\u string()[function.mysql real escape string]:第46行article.php中用户“root”@“localhost”(使用密码:否)的访问被拒绝 警告:mysql\u real\u escape\u string()[function.mysql real escape string]:无法在第46行的article.php中建立到服务器的链接 警告:mysql_query()[function.mysql query]:第61行article.php中用户“root”@“localhost”(使用密码:否)的访问被拒绝 警告:mysql\u query()[function.mysql query]:无法在第61行的article.php中建立到服务器的链接 警告:mysql\u fetch\u assoc()希望参数1是资源,布尔值在article.php第64行给出 有人知道这是什么意思吗 my search.php代码如下:search.php页面返回错误,php,mysql,search,Php,Mysql,Search,我正在为我的网站创建一个搜索页面 这是我从 在搜索框中输入任何内容并点击搜索时,我会收到一些错误信息,如: **警告:mysql\u real\u escape\u string()[function.mysql real escape string]:第46行article.php中用户“root”@“localhost”(使用密码:否)的访问被拒绝 警告:mysql\u real\u escape\u string()[function.mysql real escape string]:无
<?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>
<?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;
}
}
?>
搜索Xclo
搜寻
替换:
echo "<h3>($result['title'])</h3>";
echo "<p>($result['body'])</p>";
echo“($result['title'])”;
echo“($result['body'])”;
与:
echo“{$result['title']}”;
echo“{$result['body']}”;
更换:
echo "<h3>($result['title'])</h3>";
echo "<p>($result['body'])</p>";
echo“($result['title'])”;
echo“($result['body'])”;
与:
echo“{$result['title']}”;
echo“{$result['body']}”;
问题出在
echo "<h3>($result['title'])</h3>";
echo“($result['title'])”;
问题是,除非变量与echo连接,否则php将无法正确解析该变量
echo "<h3>(".$result['title'].")</h3>";
echo“(“$result['title']”);
或者用{}封装
echo "<h3>({$result['title']})</h3>";
echo“({$result['title']})”;
问题出在
echo "<h3>($result['title'])</h3>";
echo“($result['title'])”;
问题是,除非变量与echo连接,否则php将无法正确解析该变量
echo "<h3>(".$result['title'].")</h3>";
echo“(“$result['title']”);
或者用{}封装
echo "<h3>({$result['title']})</h3>";
echo“({$result['title']})”;
试试看
其中,$con
定义您的mysql连接。试试看
其中,$con
定义您的mysql连接。在使用mysql\u real\u escape\u字符串之前,您必须与mysql服务器建立一个连接
见:或
编辑:尝试以下操作(未测试):
编辑:哦,对不起,试试这个;-)
在使用MySql\u real\u escape\u字符串之前,您必须与MySql服务器建立一个连接
见:或
编辑:尝试以下操作(未测试):
编辑:哦,对不起,试试这个;-)
您能显示echo$sql的输出吗代码>请编辑您的问题并在此处包含错误消息。SO的工作原理是记录如何解决问题如果错误被修复,没有人能看到问题是什么,那么这个问题对用户毫无益处,因此用户只能通过简单地回显$sql查询来知道问题在哪里,或者将其粘贴到此处可能是Yogesh Suthar的副本,这与此无关。您能否显示回显$sql的输出代码>请编辑您的问题并在此处包含错误消息。SO的工作原理是记录如何解决问题如果错误被修复,没有人能看到问题是什么,那么这个问题对用户来说毫无益处,因为用户只能通过简单地回显$sql查询来知道问题在哪里,或者将问题粘贴到此处可能是Yogesh Suthar的副本,这与此无关。这修复了解析错误。接下来,您需要修复搜索帖子功能。列名和表名必须用“”转义,不能用“”转义。这修复了分析错误。接下来,您需要修复搜索帖子功能。列名和表名必须用“”转义,不能用“”转义。我已建立连接。它存储在“include_once('include/connection.php');”中。问题是它为什么不工作?我的连接页面在我网站的其余部分工作。现在怎么突然不正确了?如果我发布连接代码会有帮助吗?但是您在search_posts()中不使用PDO。那么我应该在search_posts()中添加什么来连接到PDO?谢谢。我将编辑我的文章页面,以反映其当前显示,但我现在只得到1个错误。这是一个致命错误:在article.php第64I行的非对象上调用成员函数fetchAll(),我已建立连接。它存储在“include_once('include/connection.php');”中。问题是它为什么不工作?我的连接页面在我网站的其余部分工作。现在怎么突然不正确了?如果我发布连接代码会有帮助吗?但是您在search_posts()中不使用PDO。那么我应该在search_posts()中添加什么来连接到PDO?谢谢。我将编辑我的文章页面,以反映其当前显示,但我现在只得到1个错误。这是一个致命错误:在第64行的article.php中的非对象上调用成员函数fetchAll()。您的article.php页面中没有包含connection.php。因为这篇文章只是一个功能页面,并没有人会登陆它,所以这并不重要。搜索页面不会同时调用连接和文章。所以,连接将被读取两次是毫无意义的。这可能会导致更多问题。您的文章中没有包含connection.php。因为这篇文章只是一个功能页面,并没有人会登陆它,所以这并不重要。搜索页面不会同时调用连接和文章。所以,连接将被读取两次是毫无意义的。这可能会导致更多的问题。
echo "<h3>($result['title'])</h3>";
echo "<p>($result['body'])</p>";
$keywords = preg_split('#\s+#',mysql_real_escape_string($term, $con));
function search_posts($term)
{
global $pdo;
$keywords = preg_split('#\s+#', $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}";
return $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}
function search_posts($term)
{
global $pdo;
$keywords = preg_split('#\s+#', $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}";
return $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);
}