我让搜索表单从 MySQL 中的多个其他表中查询一个内部连接表。我正在使用带有 Xampp/Apache 的本地 Intranet 当我在 PHPMyAdmin 中运行 SQL 时,它运行 Perfect 并产生完全符合我希望它们出现在我的网站上的结果。
我的数据库名称是:loodt 我的表格是:ee、er、aa、adjuster 和律师。我在 ee 表上有其他表的外键(ee.er_id = er.id)等...
但是,当我尝试在我的网站上运行搜索时,它会弹出一条错误消息“无法查询数据库以获取搜索结果,MYSQL 错误表'loodt.ee a”不存在。”所以我尝试取消快捷方式字母[在下面的代码中引用](A、B、C、D< E 和 F)并输入 ee.name、er.name 等...而不是分配这些字母,它也不起作用。
在几天前开始修改它之前,我已经使用搜索引擎从单个表中查询结果,所以我知道它可以工作,并且必须是“临时”表的问题,当然我缺乏知识。
这是搜索引擎的javascript:
$(function(){
//Hide the result list on load
$('#results-list').hide();
//Click event when search button is pressed
$('#search').click(function(){
doSearch();
});
//Keypress event to see if enter was pressed in text input
$('#text').keydown(function(e){
if(e.keyCode == 13){
doSearch();
}
});
});
>
function doSearch() {
var searchText = $('#text').val();
//Rehide the search results
$('#results-list').hide();
$.ajax({
url: './search.php',
type: 'POST',
data: {
'text': searchText
},
beforeSend: function(){
//Lets add a loading image
$('#results-holder').addClass('loading');
},
success: function(data) {
//Take the loading image away
$('#results-holder').removeClass('loading');
//Was everything successful, no errors in the PHP script
if (data.success) {
//Clear the results list
$('#results-list').empty();
//Display the results
//Check to see if there are any results to display
if(data.results.length > 0) {
//Loop through each result and add it to the list
$.each(data.results, function(){
//Give the list element a rel with the data results ID incase we want to act on this later, like selecting from the list
$('#results-list').append(
"<li rel='" + this.A_name + "'>" + this.D_name + " | " + this.A_file_no + " | " + this.A_claim_no + " | " + this.A_adj_no + " | " + this.F_acronym + " | " + this.A_doi + " | " + this.A_opened + " | " + this.A_status + " | " + this.A_redwells + " | " + this.E_firm_name + " | " + this.B_name + " | " + this.C_initials + " </li>");
});
} else {
//If there are no results, inform the user - add 'no-results' class so we can style it differently
$('#results-list').append("<li class='no-results'>Your search did not return any results</li>");
}
//Show the results list
$('#results-list').fadeIn();
} else {
//Display the error message
alert(data.error);
}
}
});
}
上面的 javascript 文件是从我的标题中的索引页调用的,不确定这是否重要。这是在上面的 javascript 代码中调用的 search.php 文件。
> <?php
//Prepare an array to hold data we are going to send back to the jQuery
$data = array(
'results' => array(),
'success' => false,
'error' => ''
);
//Has the text been posted?
if (isset($_POST['text'])) {
//Connect to the database
>
$dbhost = 'localhost'; //hostname
$dbuser = 'xxxx'; //database username
$dbpass = 'xxxxx'; //database password
$dbname = 'loodt'; //database name
>
//Create the connection to the mySQL database or catch the exception if there is an error
$db = new mysqli($dbhost, $dbuser, $dbpass);
>
$db->select_db($dbname);
>
if($db->connect_errno > 0){
die('ERROR! - COULD NOT CONNECT TO mySQL DATABASE: ' . $db->connect_error);
}
>
> //Escape the text to prevent SQL injection
$text = $db->real_escape_string($_POST['text']);
//Run a LIKE query to search for titles that are like the entered text
>
$q = "SELECT `A.name AS A_name`, `D.name AS D_name`, `A.file_no AS A_file_no`, `A.claim_no AS A_claim_no`,
`A.adj_no AS A_adj_no`, `F.acronym AS F_acronym`, `A.doi AS A_doi`, `A.opened AS A_opened`,
`A.status AS A_status`, `A.redwells AS A_redwells`, `C.initials AS C_initials`, `B.name AS B_name`, `E.firm_name AS E_firm_name`
FROM `ee A`
INNER JOIN `adjuster B` ON `A.adjuster_id` = `B.id`
INNER JOIN `attorney C` ON `A.attorney_id` = `C.id`
INNER JOIN `er D` ON `A.er_id` = `D.id`
INNER JOIN `aa E` ON `A.aa_id` = `E.id`
INNER JOIN `wcab F` ON `A.wcab_id` = `F.id`
>
WHERE
`A_name` LIKE '%{$text}%' OR `A_file_no` LIKE '%{$text}%' OR `A_claim_no` LIKE '%{$text}%' OR `A_adj_no` LIKE '%{$text}%' OR `A_doi` LIKE '%{$text}%'";;
$result = $db->query($q);
//Did the query complete successfully?
if (!$result) {
//If not add an error to the data array
$data['error'] = "Could not query database for search results, MYSQL ERROR: " . $db->error;
} else {
//Loop through the results and add to the results array
while ($row = $result->fetch_assoc()) {
$data['results'][] = array(
'A_name' => $row['A_name'],
'D_Name' => $row['D_name'],
'A_file_no' => $row['A_file_no'],
'A_claim_no' => $row['A_claim_no'],
'A_adj_no' => $row['A_adj_no'],
'F_acronym' => $row['F_acronym'],
'A_doi' => $row['A_doi'],
'A_opened' => $row['A_opened'],
'A_status' => $row['A_status'],
'A_redwells' => $row['A_redwells'],
'C_initials' => $row['C_initials'],
'B_name' => $row['B_name'],
'E_firmname' => $row['E_firmname']
);
}
//Everything went to plan so set success to true
$data['success'] = true;
}
}
//Set the content type for a json object and ensure charset is UTF-8. NOt utf8 otherwise it will not work in IE (Darn IE! >.<)
header("Content-Type: application/json; charset=UTF-8");
//json encode the data and cast to an object so we can reference items like this.id in the javascript instead of this['id'] etc.
echo json_encode((object)$data);
?>
这是 SQl 查询的图像和列名的链接: