March 10, 2013

Search code error

Question by IMEzzat

I have a php/ mysql search code like the following:

error_reporting(E_ALL);
ini_set('display_errors', '1');

$search_output = "";
if(isset($_POST['searchquery']) && $_POST['searchquery'] != ""){
    $searchquery = preg_replace('#[^a-z 0-9?!]#i', '', $_POST['searchquery']);

    if($_POST['filter'] == "user"){
        $sqlCommand = "SELECT id, username AS title FROM users 
          WHERE MATCH (username,email) AGAINST ('$searchquery')";
    } else if($_POST['filter'] == "email"){
        $sqlCommand = "SELECT id, blog_title AS title FROM blog 
          WHERE MATCH (blog_title,blog_body) AGAINST ('$searchquery')";
    }

    include_once("php_includes/db_conx.php");

    $query = mysql_query($sqlCommand) or die(mysql_error());

    $count = mysql_num_rows($query);
    if($count > 1){
        $search_output .= 
          "<hr />$count results for <strong>$searchquery</strong>
           <hr />$sqlCommand<hr />";

        while($row = mysql_fetch_array($query)){
            $id = $row["id"];
            $title = $row["title"];
            $search_output .= "Item ID: $id - $title<br />";
        } // close while
    } else {
        $search_output = 
          "<hr />0 results for <strong>$searchquery</strong>
           <hr   />$sqlCommand";
    }
}

When I run this code I got the following errors:

Warning: mysql_query() [function.mysql-query]: Access denied for user 'famebox'@'localhost' (using password: NO) in /home/famebox/public_html/search.php on line 15

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in /home/famebox/public_html/search.php on line 15
Access denied for user 'famebox'@'localhost' (using password: NO)

Answer by Starx

Access denied for user ‘famebox’@’localhost’ (using password: NO)

Your database username and password and possibly the database name is incorrect. Check these details and verify them.

Above error message says you haven’t specified a password.

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!