search.php页面带回错误

时间:2013-07-31 11:26:21

标签: php mysql search

我可以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中给出布尔值

任何人都可以看到它的含义吗?

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

?>

我可以将哪些内容添加到此页面以使其正常工作?

感谢。

4 个答案:

答案 0 :(得分:1)

替换:

echo "<h3>($result['title'])</h3>";
echo "<p>($result['body'])</p>";

使用:

echo "<h3>{$result['title']}</h3>";
echo "<p>{$result['body']}</p>";

答案 1 :(得分:1)

尝试

echo "<h3>{$result['title']}</h3>";
echo "<p>{$result['body'])}</p>";

或尝试

echo "<h3>".$result['title']."</h3>";
echo "<p>".$result['body']."</p>";

而不是

echo "<h3>($result['title'])</h3>";
echo "<p>($result['body'])</p>";

修改

现在针对您的更新问题,正如错误所示,您没有连接到mysql数据库来执行相关操作。

您可以尝试将链接标识符传递给mysql_real_escape_string函数,如

$keywords = preg_split('#\s+#',mysql_real_escape_string($term, $con));

其中$con定义了你的mysql连接。

答案 2 :(得分:1)

在使用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);
}

答案 3 :(得分:0)

问题在于

echo "<h3>($result['title'])</h3>";

问题是除非变量与echo

连接,否则php不会正确解析它
echo "<h3>(".$result['title'].")</h3>";

或用{}

封装
echo "<h3>({$result['title']})</h3>";