我正在为我的网站使用实时AJAX搜索。
我从ninetofive.me/data/tutorials/live-search/
下载了脚本。
当我搜索时,我收到致命错误:Call to a member function fetch_array() on a non-object in E:\xampp\htdocs\test\ajaxsearch\search.php on line 54
。
除了更改MySQL凭据之外,我没有对演示脚本进行任何更改。
这是我的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\n", $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);
}
*/
?>
请帮帮我:
答案 0 :(得分:1)
在使用之前,您必须检查 mysqli-&gt;查询方法返回的内容。
如果SQL查询中存在错误,查询方法应返回 FALSE ,在这种情况下,您可以询问myslqi 的错误是什么错误属性。
您的代码应该是这样的:
$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;
}
}