mysql - search.php page bringing back errors -


im creating search page site can see here

which got this tutorial

when entering in search box , hitting search im getting few errors saying:

**warning: mysql_real_escape_string() [function.mysql-real-escape-string]: access denied user 'root'@'localhost' (using password: no) in article.php on line 46

warning: mysql_real_escape_string() [function.mysql-real-escape-string]: link server not established in article.php on line 46

warning: mysql_query() [function.mysql-query]: access denied user 'root'@'localhost' (using password: no) in article.php on line 61

warning: mysql_query() [function.mysql-query]: link server not established in article.php on line 61

warning: mysql_fetch_assoc() expects parameter 1 resource, boolean given in article.php on line 64

can see means?

my search.php code this:

<?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 $result){       echo "<h3>($result['title'])</h3>";       echo "<p>($result['body'])</p>";           }  } ?> </div> </body>  </html> 

and article.php this:

    <?php  class storearticle { public function fetch_all(){     global $pdo;       $query = $pdo->prepare("select * mobi");       $query->execute(); return $query->fetchall();               }  public function fetch_data($promo_title) {    global $pdo;   $query = $pdo->prepare("select * mobi 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` mobi");       $query->execute(); return $query->fetchall();               }  public function fetch_data($promo_cat) {    global $pdo;   $query = $pdo->prepare("select distinct * mobi `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' '%" . implode("%' or 'promo_title' '%", $keywords) . "%'"; $content_where   = "'promo_content' '%" . implode("%' or 'promo_content' '%", $keywords) . "%'"; $name_where   = "'promo_name' '%" . implode("%' or 'promo_name' '%", $keywords) . "%'";   $sql = "select                'promo_title' 'title',               left('promo_content', 100) 'body',               'promo_name' 'name'               'mobi'               {$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; } }    ?> 

my connection.php code this:

<?php  try { $pdo = new pdo('mysql:host=localhost;dbname=xclo', 'xclo', 'xclo'); }catch (pdoexception $e){   exit('database error.'); }  ?> 

what can add page make work?

thanks.

you have make 1 connection mysql server before using mysql_real_escape_string.

see: http://php.net/manual/de/book.mysql.php or http://php.net/manual/de/book.pdo.php

edit: try (not tested):

function search_posts($term) {   global $pdo;    $keywords = preg_split('#\s+#', $term);    $title_where = "'promo_title' '%" . implode("%' or 'promo_title' '%", $keywords) . "%'";   $content_where = "'promo_content' '%" . implode("%' or 'promo_content' '%", $keywords) . "%'";   $name_where = "'promo_name' '%" . implode("%' or 'promo_name' '%", $keywords) . "%'";    $sql = "select           'promo_title' 'title',           left('promo_content', 100) 'body',           'promo_name' 'name'           'mobi'           {$title_where}           or {$content_where}           or {$name_where}";    return $pdo->query($sql)->fetchall(pdo::fetch_assoc); } 

edit: oh sorry, try ;-)

function search_posts($term) {   global $pdo;    $keywords = preg_split('#\s+#', $term);    $title_where = "`promo_title` '%" . implode("%' or `promo_title` '%", $keywords) . "%'";   $content_where = "`promo_content` '%" . implode("%' or `promo_content` '%", $keywords) . "%'";   $name_where = "`promo_name` '%" . implode("%' or `promo_name` '%", $keywords) . "%'";    $sql = "select           `promo_title` 'title',           left(`promo_content`, 100) 'body',           `promo_name` 'name'           `mobi`           {$title_where}           or {$content_where}           or {$name_where}";    return $pdo->query($sql)->fetchall(pdo::fetch_assoc); } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -