使用php和mysql从两个表中搜索

时间:2014-07-09 14:32:17

标签: php mysql

我有这个代码并且工作正常,但我添加了新的数据库表 我想在同一时间从两个表中搜索,我的新mysql表是 old_customers,包含字段firstname和lastname。可以帮助我如何更改代码

// 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 new_customers WHERE firstname LIKE "%'.$search_string.'%" OR lastname 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['lastname']);
        $display_name = preg_replace("/".$search_string."/i", "<b class='highlight'>".$search_string."</b>", $result['firstname']);
        $display_url = 'faturimi.php?user='.urlencode($result['username']).'&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 result found.</b>', $output);
    $output = str_replace('functionString', 'try again', $output);

    // Output
    echo($output);
}

}

0 个答案:

没有答案