douhua1760 2014-11-20 05:00
浏览 19
已采纳

在实时ajax搜索中搜索时出现致命错误

I'm using a live AJAX search for my website.

I downloaded the script from ninetofive.me/data/tutorials/live-search/.

When I search, I get Fatal error: Call to a member function fetch_array() on a non-object in E:\xampp\htdocs\test\ajaxsearch\search.php on line 54.

I didn't make any changes in the demo script other than changing the MySQL credentials.

Here is my search.php:

<?php
// Credentials
$dbhost = "localhost";
$dbname = "testdb";
$dbuser = "root";
$dbpass = "";

//  Connection
global $tutorial_db;

$tutorial_db = new mysqli();
$tutorial_db->connect($dbhost, $dbuser, $dbpass, $dbname);
$tutorial_db->set_charset("utf8");

//  Check Connection
if ($tutorial_db->connect_errno) {
    printf("Connect failed: %s
", $tutorial_db->connect_error);
    exit();
    }

/************************************************
    Search Functionality
************************************************/

// 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 = $tutorial_db->real_escape_string($search_string);

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

    // Do Search
    $result = $tutorial_db->query($query);
    while($results = $result->fetch_array()) {
        $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);
    }
}


/*
// Build Function List (Insert All Functions Into DB - From PHP)

// Compile Functions Array
$functions = get_defined_functions();
$functions = $functions['internal'];

// Loop, Format and Insert
foreach ($functions as $function) {
    $function_name = str_replace("_", " ", $function);
    $function_name = ucwords($function_name);

    $query = '';
    $query = 'INSERT INTO search SET id = "", function = "'.$function.'", name = "'.$function_name.'"';

    $tutorial_db->query($query);
}
*/
?>

Please help me:

展开全部

  • 写回答

1条回答 默认 最新

  • douya1061 2014-11-20 05:14
    关注

    You have to check what mysqli->query methods return before using it.

    If there is an error in your SQL query, the query methods should return FALSE and in this case you can ask myslqi what is the error with the error property.

    Your code should be like this :

    $result = $tutorial_db->query($query);
    if($result === FALSE) {
        echo $tutorial_db->error;
        // or do whatever you have to do to fix or log the issue.
    }
    else {
        while($results = $result->fetch_array()) {
            $result_array[] = $results;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥100 有能够实现人机模式的c/c++代码,有图片背景等,能够直接进行游戏
  • ¥20 校园网认证openwrt插件
  • ¥15 以AT89C51单片机芯片为核心来制作一个简易计算器,外部由4*4矩阵键盘和一个LCD1602字符型液晶显示屏构成,内部由一块AT89C51单片机构成,通过软件编程可实现简单加减乘除。
  • ¥15 某东JD算法逆向算法
  • ¥15 求GCMS辅导数据分析
  • ¥30 SD中的一段Unet下采样代码其中的resnet是谁跟谁进行残差连接
  • ¥15 Unet采样阶段的res_samples问题
  • ¥60 Python+pygame坦克大战游戏开发实验报告
  • ¥15 R语言regionNames()和demomap()无法选中中文地区的问题
  • ¥15 Open GL ES 的使用
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部