我正在使用此查询在数据库中搜索三列的值。在我输入值跨越多个列的查询之前,这种方法非常有效。
例如,如果我输入一个名字和一个公司“ John McDonalds”,结果将返回空白。有没有一种方法可以允许同时搜索多个列?
string xml = "<Country>MyCountry</Country><Prefecture>MyPrefecture</Prefecture><City>MyCity</City>";
string wrapped = $"<Address>{xml}</Address>";
XmlSerializer serializer = new XmlSerializer(typeof(Address));
Address addr = (Address)serializer.Deserialize(new StringReader(wrapped));
答案 0 :(得分:0)
当您要搜索多个术语时,一种选择是拆分传入的$_POST['search']
,然后多次搜索数据库。这意味着您的搜索算法复杂度乘以n(字数)。
if (isset($_POST['search']) && $_POST['search'] != '') {
$search_terms = explode( ' ', $_POST['search'] );
$results = array();
foreach( $search_terms as $search_term ) {
array_push( $results, search_db( $search_term ) );
}
header("Content-Type: application/json");
echo json_encode($result_array);
}
function search_db( $search_term ) {
global $conn;
$stmt = $conn->prepare("SELECT * FROM tickets_info WHERE (name LIKE CONCAT('%', ?, '%')) OR (company LIKE CONCAT('%', ?, '%')) OR (ticket LIKE CONCAT('%', ?, '%'))");
$stmt->bind_param('sss', search_term, search_term, search_term );
$stmt->execute();
$result = $stmt->get_result();
$result_array = array();
while ($row = $result->fetch_assoc()) {
array_push($result_array, $row);
}
$stmt->close();
return $result_array;
}