3

I'm trying to use autocomplete using php, mysql and jquery. However, I get an error in PHP code that says:

Fatal error: Call to a member function bind_param() on a non-object in C:\Users\Admin\Documents\USBWebserver v8.6\root\kk.php on line 8

This is my php code:

<?php

include 'config.php'; // this works fine

$keyword = '%'.$_POST['keyword'].'%';
$sql = "SELECT * FROM person WHERE firstName LIKE (:keyword)";
$query = $conn->prepare($sql);
$query->bind_param(':keyword', $keyword); // THIS IS LINE 8 WHERE THE ERROR IS COMING FROM
$query->execute();
$list = $query->fetchAll();
foreach ($list as $rs) {
    // put in bold the written text
    $fname = str_replace($_POST['keyword'], '<b>'.$_POST['keyword'].'</b>', $rs['firstName']);
    // add new option
    echo '<li onclick="set_item(\''.str_replace("'", "\'", $rs['firstName']).'\')">'.$fname.'</li>';
}
?>

This is the javascript file:

<script>

function auto_complete() {
    var min_length = 0;
    var keyword = $('#names_id').val();
    if (keyword.length >= min_length) {
        $.ajax({
            url: 'kk.php',
            type: 'POST',
            data: {keyword:keyword},
            success:function(data){
                $('#names_list_id').show();
                $('#names_list_id').html(data);
            }
        });
    } else {
        $('#names_list_id').hide();
    }
}

function set_item(item) {
    // change input value
    $('#names_id').val(item);
    // hide proposition list
    $('#names_list_id').hide();
}
    </script>

I want to get the first name, middle name and last name.

This wasn't taught in school and we need it for our project. I got the code from http://www.bewebdeveloper.com/tutorial-about-autocomplete-using-php-mysql-and-jquery. Can you all please help me? Thank you.

Sanjuktha
  • 1,065
  • 3
  • 13
  • 26
Isabella
  • 455
  • 1
  • 10
  • 23
  • Your prepare() failed. You have to check the return value of `$conn->prepare(...)` (i.e. $query). If it's false/null then an error occured and `$conn->error_list` can tell you more details about the error, see http://docs.php.net/manual/en/mysqli.error-list.php – VolkerK Dec 28 '15 at 06:43
  • where is your code for $conn variable defined?? – Manoj Salvi Dec 28 '15 at 06:53
  • @ManojSalvi it's in the config.php – Isabella Dec 28 '15 at 07:06

0 Answers0