gpt4 book ai didi

php - search.php 页面返回错误

转载 作者:太空宇宙 更新时间:2023-11-03 12:25:22 25 4
gpt4 key购买 nike

我正在尽可能地为我的网站创建一个搜索页面 SEE HERE

我从this tutorial得到的

当在搜索框中输入任何内容并点击搜索时,我会收到一些错误提示:

**警告:mysql_real_escape_string() [function.mysql-real-escape-string]:第 46 行 article.php 中的用户 'root'@'localhost'(使用密码:NO)的访问被拒绝

警告:mysql_real_escape_string() [function.mysql-real-escape-string]:无法在第 46 行的 article.php 中建立到服务器的链接

警告:mysql_query() [function.mysql-query]:第 61 行 article.php 中用户 'root'@'localhost'(使用密码:NO)的访问被拒绝

警告:mysql_query() [function.mysql-query]:无法在第 61 行的 article.php 中建立到服务器的链接

警告:mysql_fetch_assoc() 期望参数 1 为资源,第 64 行 article.php 中给出的 bool 值

谁能看出这是什么意思?

我的 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.');
}

?>

我可以向该页面添加什么以使其正常工作?

谢谢。

最佳答案

在使用 mysql_real_escape_string 之前,您必须与 MySql 服务器建立一个连接。

参见:http://php.net/manual/de/book.mysql.phphttp://php.net/manual/de/book.pdo.php

编辑:试试这个(未测试):

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);
}

关于php - search.php 页面返回错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17969029/

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