当DB中只有一个存在时返回多个结果

时间:2013-07-25 01:32:05

标签: php mysql pdo

好的,所以这段代码在过去的一天左右给了我麻烦。除税务查询外,它的所有职责都非常合理。 (至少那是我唯一能给你带来麻烦的人)...... 据我所知;代码给我带来了麻烦,因为它们是多个TUID和UID是相同的,但不是去同一个用户,(EG是一个ID = = 1可能是User1,也可能是Group1)。

我不是要求你们修这个代码,(似乎每次我发布这里,有人说'哦,你只是想让我们修改你的代码,等等等等等等等等)我只是好奇,如果有人为此做了很好的工作?

我收到了返回查询的重复行; 这是查询返回的内容:http://gmz1023.com/rowline.PNG

{
    $sql = "SELECT type FROM bank_transaction LIMIT 25";
    $que = $this->db->prepare($sql);
    $que->bindParam('uid', $uid);
    try{ 
    $que->execute(); 
    ob_start();
    if($que)
    {
        $id = '1';
        $transaction = '';
        while($row = $que->fetch())
        {
        /* This is the tricky bit; for each type that is returned, we start another query and pull up that data.<
        /* This may need to be turned into seperate functions later on. however, for now, keep trying to figure out how to get the code to work, 
        /* There's      nor eason why it shouldn't work, as its simple as hell. and yet we keep getting multiple rows in the table.
            */ 
            if($row[0] == 'govt')
            {
                $sql = "SELECT tuid, uid, balance FROM bank_transaction WHERE type = :type AND uid = :uid";
                $querty1 = $this->db->prepare($sql);
                $type = 'govt';

                $querty1->bindParam('type', $type);
                $querty1->bindParam('type', $row[0]);
                $querty1->bindParam('uid', $uid);
                try {
                    if($querty1->execute())
                    {
                        $info = $querty1->fetch();
                        $to = parent::getUsername($info[0]);
                        $from = parent::getStoryName($info[1]);
                        $balance = $info[2];
                        $transaction .= "<tr><td>{$id}</td><td>{$from}</td><td>{$to}</td><td>{$balance}</td><td>{$row[0]}</td></tr>";
                        $querty1->closeCursor();
                    }
                }catch(PDOException $e) { echo $e->getMessage();}
            }
            if($row[0] == 'business')
            {
                $sql = "SELECT tuid, uid, balance, date FROM bank_transaction WHERE type = :type AND tuid = :uid OR uid = :uid";
                $querty1 = $this->db->prepare($sql);
                $type = 'business';
                $querty1->bindParam('type', $type);
                $querty1->bindParam('type', $row[0]);
                $querty1->bindParam('uid', $uid);
                try {
                    if($querty1->execute())
                    {
                        $info = $querty1->fetch();
                        $to = $info[0];
                        $from = $info[1];
                        $balance = $info[2];
                        $date = $info[3];
                        $transaction .= "<tr><td>{$id}</td><td>{$from}</td><td>{$to}</td><td>{$balance}</td><td>{$row[0]}</td><td>{$info[3]}</tr>";
                        $querty1->closeCursor();
                    }
                }catch(PDOException $e) { echo $e->getMessage();}
            }
            if($row[0] == 'tax')
            {

                $sql = "SELECT tuid, uid, balance, date FROM bank_transaction WHERE tuid = :tuid AND type = :type ;";
                $querty = $this->db->prepare($sql);
                $type = 'tax';
                $uid = '2';
                $querty->bindParam('type', $type);
                $querty->bindParam('tuid', $uid);
                try {
                    if($querty->execute())
                    {
                        $info = $querty->fetch();
                        $to = parent::getStoryName($info[0]);
                        $from = parent::getUsername($info[1]);
                        $balance = $info[2];
                        $transaction .= "<tr><td>{$id}</td><td>{$from}</td><td>{$to}</td><td>{$balance}</td><td>{$row[0]}</td><td>{$info[3]}</tr>";
                        $querty->closeCursor();
                    }
                }catch(PDOException $e) { echo $e->getMessage();}

            }
            elseif($row[0] == 'personal')
            {
                $sql = "SELECT tuid, uid, balance FROM bank_transaction WHERE type = :type AND uid = :uid OR tuid = :uid";
                $querty = $this->db->prepare($sql);
                $type = 'personal';
                $querty->bindParam('type', $type);
                $queryt->bindParam('uid', $uid);
                try {
                    if($querty->execute())
                    {
                        $info = $querty->fetch();
                        $to = $info[0];
                        $from = $info[1];
                        $balance = $info[2];
                        $transaction .= "<tr><td>{$id}</td><td>{$from}</td><td>{$to}</td><td>{$balance}</td><td>{$row[0]}</td></tr>";
                        $querty->closeCursor();
                    }
                }catch(PDOException $e) { echo $e->getMessage();}
            }
            $id = $id +1;
        }
    return $transaction;
    ob_end_clean();
    }
    else
    {
        echo "ERROR!";
    }
    }catch(PDOException $e) { echo $e->getMessage(); }  
}

数据库     tid int(11)否无AUTO_INCREMENT     uid int(11)否无     tuid int(11)否无     balance int(11)否无     type enum('personal','govt','business','tax')latin1_swedish_ci没有日期datetime

2 个答案:

答案 0 :(得分:1)

税务查询最后有分号。这很可能导致MySQL抛出错误。


但整个代码示例很奇怪。显示的$id值不是来自数据库,它是从第一个查询获取的行的循环计数器。

SELECT type FROM bank_transaction LIMIT 25

对于返回的每一行,将对“type”返回的值执行检查,并根据值执行其他四个查询之一。查询都是类似的:

<强> "govt"

SELECT tuid
     , uid
     , balance
     , date
  FROM bank_transaction
 WHERE type = :type
   AND tuid = :uid
    OR uid = :uid

<强> "business"

SELECT tuid
     , uid
     , balance
     , date
  FROM bank_transaction
 WHERE type = :type
   AND tuid = :uid
    OR uid = :uid

<强> "tax"

SELECT tuid
     , uid
     , balance
     , date
  FROM bank_transaction
 WHERE tuid = :tuid
   AND type = :type

<强> "personal"

SELECT tuid
     , uid
     , balance
  FROM bank_transaction
 WHERE type = :type
   AND uid = :uid 
    OR tuid = :uid

ANDOR在某些查询中的优先级存在一些潜在问题,并且PDO也存在潜在问题,并且多次引用相同的命名绑定变量(但是这个“错误“可能已在更新版本的PDO中得到解决。”

bindParam调用指定了没有前导冒号的命名绑定参数,这是奇数。我以前从未见过。


我认为代码的更大问题是每次通过最外层循环(它确实设法每次成功递增$id值)。但是对于每种类型,它执行相同的SQL语句,具有相同的绑定值。并且几乎可以保证,每次执行查询时,MySQL都以相同的顺序返回相同的行集。

但只提取并处理了第一行。

如果这是生硬的话我道歉,但是......

这看起来好像有人在问题上投入了大量代码,而没有真正理解需要做什么,而且没有考虑可行的算法。

此代码的问题比语法问题大很多。

从数据库获取类型的最外层查询看起来没有任何意义,除了限制返回的行数。 (我有点惊讶的是结果中没有25行;我只能猜测表中实际上有4行,或者更有可能的是,该查询返回的行数超过4行,但返回的第5行有type='tax',这导致执行“tax”查询,导致MySQL语法错误。

这段代码根本不是“简单”。它过于复杂,并且毫无疑问无效。

答案 1 :(得分:0)

您可以使用GROUP BY使选择唯一:

SELECT tuid, uid, balance FROM bank_transaction WHERE type = :type AND uid = :uid GROUP BY uid