MySQL site search

I want to do a search without reloading with ajaxon jQuery, but it gives me an error. Although without ajaxeverything works fine…


Here is the script jQuery

// 
$('#search-result #search button').live('click', function () {
    $('#loader').show();
    var zapros = $('#search-result #search input').val();
    $.ajax({
        type: "POST",
        url: "include/search_result.php",
        data: ({
            searchs: zapros
        }),
        success: function (data) {
            $("#content").html(data);
            $('#loader').hide();
        }
    });
    return false;
});  

Here is the handler

if (isset($_POST['searchs'])) {
    $searchs = mysql_escape_string(strip_tags(trim($_POST['searchs'])));
    $errors = array();
    if (empty($searchs)) {
        $errors[] = '  !';
    } else if (strlen(utf8_decode($searchs)) < 3) {
        $errors[] = '    3 !';
    }
    if (empty($errors)) {
        // 
        $returned_results = array();
        $where            = "";
        $searchs      = preg_split("/[\s]+/", $searchs);
        $total_search = count($searchs);
        foreach ($searchs as $key => $search) {
            $where .= "text LIKE '%$search%'";
            if ($key != ($total_search - 1)) {
                $where .= " OR ";
            }
        }
        $results_searchs = "SELECT id,category,title,date,view,description,total_value,miniatura,(SELECT COUNT(*) FROM comments WHERE comments.lesson = lessons.id) AS com FROM lessons WHERE $where";
        $results_searchs = mysql_query($results_searchs);
        if (mysql_num_rows($results_searchs) > 0) {
            $results_num = mysql_num_rows($results_searchs);
            echo "<p style = 'margin-left: 30px;'>   <span style = 'color: #F78D1D; font-weight: bold;'>" . $_POST['searchs'] . "</span>  <span style = 'color: #F78D1D; font-weight: bold;'>" . $results_num . "</span> :</p>";
            $myrow_searchs = mysql_fetch_array($results_searchs);
            do {
                printf("
                        <div class='lesson'>
                            <img class='mini' src='%s' />
                            <a href = '/lesson.php?category=%s&id=%s'>%s</a>
                            <p>%s</p>
                            <hr>
                            <div class='info'>
                                <span><img src='/img/icon/file_add.png' width='11' height='11' /> : %s </span>
                                <span><img src='/img/icon/eye.png' width='11' height='11' /> : %s </span>
                                <span><img src='/img/icon/comment.png' width='11' height='11' /> : %s </span>
                                <span><img src='/img/icon/rating.png' width='11' height='11' /> : %s </span>
                            </div>
                        </div>
                        ", $myrow_searchs["miniatura"], $myrow_searchs["category"], $myrow_searchs["id"], $myrow_searchs["title"], $myrow_searchs["description"], $myrow_searchs["date"], $myrow_searchs["view"], $myrow_searchs["com"], $myrow_searchs["total_value"]);
            } while ($myrow_searchs = mysql_fetch_array($results_searchs));
        } else {
            echo '   ' . $_POST['searchs'] . '   !';
        }
    } else {
        foreach ($errors as $error) {
            echo $error, '<br />';
        }
    }
} else {
    echo '     !';
}

Here is the error

Warning: mysql_query() [function.mysql-query]: Access denied for user ‘ODBC’@’localhost’ (using password: NO) in Z:\home\vorobyev.ru\www\include\search_result.php on line 40

Warning: mysql_query() [function.mysql-query]: A link to the server could not be established in Z:\home\vorobyev.ru\www\include\search_result.php on line 40

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in Z:\home\vorobyev.ru\www\include\search_result.php on line 41


Answer 1

The code did not work for me, I searched for a long time and found the answer.

The reason is simple: it lies in the keyword LIMIT.

The result is not displayed if there are very many matches (i.e. hundreds of results found). In this case, you need to put a limit!

Example:

$post = mysql_query("SELECT id, title, etitle FROM info WHERE title LIKE '%%' LIMIT 10 ",$db);

Then everything will work!

Leave a reply

Please enter your comment!
Please enter your name here