dongqu2863 2013-03-18 08:22
浏览 62
已采纳

使用其他表php的列表从一个表中获取数据

I have following tables

myListTable

playerId listId type
 50       10     0
 51       10     0

players

id  x  xx etc
50  x  xx etc
51  x  xx etc

I want to run a query where i provide listId and type and it will get the lists of all players related to that listId and type

try {
    $conn = $this->GetDBConnection();
    $type = 0; // 0 REQUEST BY PLAYERS
    $statement = $conn->prepare('SELECT p.* FROM myListTable c, players p WHERE (c.listId = :listId ) AND (c.type = :type) AND ( p.id = c.playerId) ');
    $statement->bindParam(':listId', $listId, PDO::PARAM_INT);
    $statement->bindParam(':type', $type, PDO::PARAM_INT);
    $statement->execute();
    if(!($row = $statement->fetchAll(PDO::FETCH_ASSOC))) {
        return false;
    }
    $conn = null;
} catch(PDOException $e) {
    throw $e;
}

This is just returning me false. What do i have to do to fix this query?

  • 写回答

1条回答 默认 最新

  • duanqiao2006 2013-03-18 08:30
    关注

    One of the mistake is a typo in your prepare query:

    $statement = $conn->prepare('SELECT p.* 
        FROM myListTable c, players p 
        WHERE (c.listId = :c.listId ) 
            AND (c.type = :type) 
            AND ( p.id = c.playerId) ');
    

    The :c.listId should just be :listId.

    The second one is, type is a reserved word in MySQL. You need backticks to escape it inside your query.

    Another is, since you're only selecting values from the players table; a JOIN is preferred.

    The query should be:

    SELECT p.*
    FROM players p
    JOIN myListTable c
        ON (p.id = c.playerId)
    WHERE (c.listId = :c.listId)
        AND (c.`type` = :type)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据