doyhq66282 2014-02-14 13:33
浏览 111

有没有办法查询MySQL数据库两次,并将第二个查询的结果添加到与第一个查询的结果相同的数组?

I am searching the DB for users by name here. I have preg_split() looking for the space between first and last name to split them up and searching for the names separately, but I want to set it up so that if the user just puts in either first or last, it will search both the 'first_name' and 'last_name' fields for the single name put in. Ex. 'Shannon Phillips' will search first_name for Shannon and last_name for Phillips, but if I just put Phillips, I want it to search both first_name and last_name for Phillips. I'm assuming I would just query the DB twice, and somehow append the results from the second query to the end of the first.

function nameProcess(){
    if($_SESSION['search_engine_option'] == 'name'){
        $name = preg_split("/[\s,]+/", $_SESSION['search_engine_input']);
        if(empty($name['0'])){
            $first  = db_query('SELECT * FROM {tls_active_applicants} WHERE `first_name` = `'.$name['1'].'`');
            $last   = db_query('SELECT * FROM {tls_active_applicants} WHERE `last_name` = `'.$name['1'].'`');
        }elseif(empty($name['1'])){
            $first  = db_query('SELECT * FROM {tls_active_applicants} WHERE `first_name` = `'.$name['0'].'`');
            $last   = db_query('SELECT * FROM {tls_active_applicants} WHERE `last_name` = `'.$name['0'].'`');
        }   

}
  • 写回答

2条回答 默认 最新

  • dongtaochan0777 2014-02-14 13:38
    关注

    Why not to use OR ?

        if(empty($name['0'])){
            $first  = db_query('SELECT * FROM {tls_active_applicants} WHERE `first_name` = `'.$name['1'].'` OR `last_name` = `'.$name['1'].'`');
        }elseif(empty($name['1'])){
            $first  = db_query('SELECT * FROM {tls_active_applicants} WHERE `first_name` = `'.$name['0'].'` OR `last_name` = `'.$name['0'].'`');
        }   
    
    评论

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么