Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/242.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用pdo连接两个表_Php_Mysql_Join - Fatal编程技术网

Php 用pdo连接两个表

Php 用pdo连接两个表,php,mysql,join,Php,Mysql,Join,我想联接数据库中的两个表。它以“经典”的方式工作得很好,但是我的脚本中有一大堆select查询,所以我想缩短它 以下是联合sql: SELECT matches.id, matches.start_time, matches.category_id, matches.highl, first_team.tid, first_team.t_name, second_team.tid, second_team.t_name AS ft_name FROM matches

我想联接数据库中的两个表。它以“经典”的方式工作得很好,但是我的脚本中有一大堆select查询,所以我想缩短它

以下是联合sql:

SELECT
    matches.id, matches.start_time, matches.category_id, 
    matches.highl, first_team.tid, first_team.t_name, 
    second_team.tid, second_team.t_name AS ft_name 
FROM matches 
    INNER JOIN teams AS first_team ON matches.first_team_id = first_team.tid 
    INNER JOIN teams AS second_team ON matches.second_team_id = second_team.tid 
ORDER BY matches.id DESC 
    LIMIT 10
我想要实现的是这样的,但我不知道如何添加我上面复制的所有值

public function getMatches($table,$conditions = array()){
    $sql = 'SELECT ';
    $sql .= array_key_exists("select",$conditions)?$conditions['select']:'';
    $sql .= ' FROM '.$table;
    if(array_key_exists("where",$conditions)){
        $sql .= ' WHERE ';
        $i = 0;
        foreach($conditions['where'] as $key => $value){
            $pre = ($i > 0)?' AND ':'';
            $sql .= $pre.$key." = '".$value."'";
            $i++;
        }
    }

    if(array_key_exists("inner_join",$conditions)){
        $sql .= ' INNER JOIN '.$conditions['inner_join'];
    }

    if(array_key_exists("on",$conditions)){
        $sql .= ' ON '.$conditions['on'];
    }

    if(array_key_exists("as",$conditions)){
        $sql .= ' AS '.$conditions['as'];
    }

    if(array_key_exists("order_by",$conditions)){
        $sql .= ' ORDER BY '.$conditions['order_by'];
    }

    if(array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
        $sql .= ' LIMIT '.$conditions['start'].','.$conditions['limit'];
    }elseif(!array_key_exists("start",$conditions) && array_key_exists("limit",$conditions)){
        $sql .= ' LIMIT '.$conditions['limit'];
    }

    $query = $this->db->prepare($sql);
    $query->execute();

    if(array_key_exists("return_type",$conditions) && $conditions['return_type'] != 'all'){
        switch($conditions['return_type']){
            case 'count':
                $data = $query->rowCount();
            break;
            case 'single':
                $data = $query->fetch(PDO::FETCH_ASSOC);
            break;
            default:
                $data = '';
        }
    }else{
        if($query->rowCount() > 0){
            $data = $query->fetchAll();
        }
    }
    return !empty($data)?$data:false;
    }
}
以下是输出:

<div class="panel-heading">Matches</div>
    <table class="table">
        <tr>
          <th>#</th>
          <th>First Team</th>
          <th>Second Team</th>
          <th>Start Time</th>
        </tr>
        <?php
        include 'inc/functions.php';
        $db = new DB();
        $matches = $db->getMatches('matches', array('inner_join'=>'teams'), array('as'=>'first_team'), array('on'=>'matches.first_team_id = first_team.tid'), array('inner_join'=>'teams'), array('as'=>'second_team'), array('on'=>'matches.second_team_id = second_team.tid'), array('order_by'=>'matches.id'));
        if(!empty($matches)){ $count = 0; foreach($matches as $result){ $count++;?>
        <tr>
          <td><?php echo $count; ?></td>
          <td><?php echo $result['ft_name']; ?></td>
          <td><?php echo $result['t_name']; ?></td>
          <td><?php echo $result['start_time']; ?></td>
        </tr>
        <?php } }else{ ?>
        <tr><td colspan="4">No entry found!</td>
        <?php } ?>
    </table>
</div>

老实说,就我个人而言,我永远都不会明白,PHP数组的墙是如何形成的

array('inner_join'=>'teams'), array('as'=>'first_team'), 
array('on'=>'matches.first_team_id = first_team.tid'), 
array('inner_join'=>'teams'), 
array('as'=>'second_team'), 
array('on'=>'matches.second_team_id = second_team.tid'), 
array('order_by'=>'matches.id'));
甚至可以远比优雅紧凑的SQL短

SELECT
    matches.id, matches.start_time, matches.category_id, 
    matches.highl, first_team.tid, first_team.t_name, 
    second_team.tid, second_team.t_name AS ft_name 
FROM matches 
    INNER JOIN teams AS first_team ON matches.first_team_id = first_team.tid 
    INNER JOIN teams AS second_team ON matches.second_team_id = second_team.tid 
ORDER BY matches.id DESC 
    LIMIT 10
更不用说它的意义和可读性了


你是不是想省去键入一些SQL关键字,比如SELECT或FROM?认真地把一个有意义且易于理解的SQL弄得一团糟真的值得吗?

您希望我们在这里做什么?您的问题是什么?选择后的值和选择前的值。如何添加它们?matches.id,matches.start_time,matches.category_id,matches.highl,first_team.tid,first_team.t_name,second_team.tid,second_team.t_name作为ft_name真正感兴趣-您是否真的发现以这种方式构建查询比手动编译查询更容易?顺便说一下,PDO不会加入您的表。它只是将您编写的SQL查询发送到mysql。删除tagok,假设我将函数命名为“getData”,并使用非联接表。我只是写了一个例子:$users=$db->getData'accounts',array'order_by'=>'id';//php:$sql='SELECT'$sql.=array_key_existsselect,$conditions?$conditions['select']:'*'$sql.='来自'.$表;因此,您再次尝试节省一些SQL关键字。打字真的那么难吗?特别是考虑到您的用户正在用它们来换取PHP代码。为什么你想削弱你的SQL,让它成为一些有限的和胡言乱语的PHP方言呢?我明白了,你可能是对的。也许在最后,“正常”的方式更有用,但现在我想这样做。那么,你能帮我一下吗?你最好考虑一下如何使你的查询对SQL注入安全。这真的值得你和我花时间