-1

好的,所以这段代码在过去一天左右给我带来了麻烦。它的所有职责都工作得很好,除了税务查询。(至少那是我能说出的唯一给我带来麻烦的人)……据我所知;该代码给我带来了麻烦,因为它们是多个相同的 TUID 和 UID,但不要转到同一个用户,(例如 == 1 的 ID 可能是 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) No None AUTO_INCREMENT uid int(11) No None tuid int(11) No None balance int(11) No None type enum('personal', 'govt', 'business', 'tax') latin1_swedish_ci 没有日期 datetime

4

2 回答 2

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 行,但返回了第五行has type='tax',这会导致执行“tax”查询,这会导致 MySQL 语法错误。

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

于 2013-07-25T03:03:20.090 回答
0

您可以使用 GROUP BY 使选择独一无二:

SELECT tuid, uid, balance FROM bank_transaction WHERE type = :type AND uid = :uid GROUP BY uid
于 2013-07-25T01:49:21.313 回答