Ajax搜索框:MySQL错误[重复]

This question already has answers here:
                </div>
            </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/12769982/reference-what-does-this-error-mean-in-php" dir="ltr">Reference - What does this error mean in PHP?</a>
                            <span class="question-originals-answer-count">
                                (36 answers)
                            </span>
                    </div>
                    <div class="grid--cell mb0 mt4">
                        <a href="/questions/2973202/mysql-fetch-array-mysql-fetch-assoc-mysql-fetch-row-mysql-num-rows-etc" dir="ltr">mysql_fetch_array()/mysql_fetch_assoc()/mysql_fetch_row()/mysql_num_rows etc… expects parameter 1 to be resource</a>
                            <span class="question-originals-answer-count">
                                (31 answers)
                            </span>
                    </div>
            <div class="grid--cell mb0 mt8">Closed <span title="2013-10-01 14:27:53Z" class="relativetime">6 years ago</span>.</div>
        </div>
    </aside>

I'm trying to create a search box using ajax and php. For some reason I get

"Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in C:\wamp\www\livesearch\livesearch.php on line 30"

on the following code:

session_start();
include("_/inc/dbcon.php");
    $link = mysql_connect($host, $login, $pw);
    mysql_select_db($database);
    if($link){
     echo 'connect';
    }
// Define Output HTML Formating
$html = '';
$html .= '<li class="result">';
$html .= '<a target="_blank" href="urlString">';
$html .= '<h3>nameString</h3>';
$html .= '<h4>functionString</h4>';
$html .= '</a>';
$html .= '</li>';

// Get Search
$search_string = preg_replace("/[^A-Za-z0-9]/", " ", $_POST['query']);
$search_string = mysql_real_escape_string($search_string);

// Check Length More Than One Character
if (strlen($search_string) >= 1 && $search_string !== ' ') {
    // Build Query
    $query = 'SELECT * FROM sp_user WHERE function LIKE "%'.$search_string.'%" OR name LIKE "%'.$search_string.'%" OR surname LIKE "%'.$search_string.'%" OR username LIKE "%'.$search_string.'%"';

    // Do Search

    $result = mysql_query($query);
    while($results = mysql_fetch_array($result)) {
        $result_array[] = $results;
    }

    // Check If We Have Results
    if (isset($result_array)) {
        foreach ($result_array as $result) {

            // Format Output Strings And Hightlight Matches
            $display_function = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['function']);
            $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['name']);
            $display_url = 'http://php.net/manual-lookup.php?pattern='.urlencode($result['function']).'&lang=en';

            // Insert Name
            $output = str_replace('nameString', $display_name, $html);

            // Insert Function
            $output = str_replace('functionString', $display_function, $output);

            // Insert URL
            $output = str_replace('urlString', $display_url, $output);

            // Output
            echo($output);
        }
    }else{

        // Format No Results Output
        $output = str_replace('urlString', 'javascript:void(0);', $html);
        $output = str_replace('nameString', '<b>No Results Found.</b>', $output);
        $output = str_replace('functionString', 'Sorry :(', $output);

        // Output
        echo($output);
    }
</div>

You should have to use mysql_fetch_assoc() (so that you can use field name in Associate array) and you should also have to add back quotes to field name because you're using some key names like name, function etc. Reserved Words in MySQL as per suggestion from @Frederik Spang

$query = 'SELECT * FROM sp_user WHERE `function` LIKE "%'.$search_string.'%" OR `name` LIKE "%'.$search_string.'%" OR `surname` LIKE "%'.$search_string.'%" OR `username` LIKE "%'.$search_string.'%"';

And it's a good practice to use back quotes ("`") in field name while specifying field names.

You should also check whether $result has records or not using

$rows_returned = mysql_num_rows($result);