简体   繁体   English

While循环与计数查询? (PHP / MySQL的)

[英]While Loop with Count Query? (PHP/MySQL)

How can I use a while loop with a COUNT query? 如何对COUNT查询使用while循环? In the series of subqueries below, I assigned each table a static value (eg 'PX' AS MySite2, 'GZ' AS MySite2)... 在下面的一系列子查询中,我为每个表分配了一个静态值(例如“ PX” AS MySite2,“ GZ” AS MySite2)...

 $sql = "SELECT SUM(num) as num FROM (
  SELECT 'PX' AS MySite2, COUNT(URL) AS num FROM pox_topics WHERE URL = :MyURL
  UNION ALL
  SELECT 'PX' AS MySite2, COUNT(URL) AS num FROM people WHERE URL = :MyURL
  UNION ALL
  SELECT 'GZ' AS MySite2, COUNT(Taxon) AS num FROM gz_life WHERE Taxon = :MyURL
  ) AS X";

$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL,PDO::PARAM_STR);
$stmt->execute();
$Total = $stmt->fetch();

To transform MySite2 into a variable ($MySite2), I simply put it inside a while loop... 要将MySite2转换为变量($ MySite2),我只需将其放入while循环中...

while ($row = $stmt->fetch())
{
 $MySite2 = $row['MySite2'];
}

switch($Total['num'])
{
 case 1:

But it isn't working. 但这不起作用。 I probably just need to modify while ($row = $stmt->fetch()) , but I haven't found the right alternative yet. 我可能只需要修改while ($row = $stmt->fetch()) ,但是我还没有找到正确的选择。 The other possibility is that while loops don't work with COUNT queries. 另一种可能性是while循环不适用于COUNT个查询。 Either way, can someone tell me what the solution is? 不管怎样,有人可以告诉我解决方案是什么?

i dont think your query return the column MySite2. 我不认为您的查询返回MySite2列。 try this query. 试试这个查询。

SELECT x.MySite2, SUM(num) as num FROM (
SELECT 'PX' AS MySite2, COUNT(URL) AS num FROM pox_topics WHERE URL = :MyURL
UNION ALL
SELECT 'PX' AS MySite2, COUNT(URL) AS num FROM people WHERE URL = :MyURL
UNION ALL
SELECT 'GZ' AS MySite2, COUNT(Taxon) AS num FROM gz_life WHERE Taxon = :MyURL
) AS X group by x.MySite2

Here's what I think you're trying to achieve... 这就是我想您要实现的目标...

$sql = "SELECT 'PX' AS MySite2, COUNT(URL) AS num FROM pox_topics WHERE URL = :MyURL
  UNION ALL
  SELECT 'PX' AS MySite2, COUNT(URL) AS num FROM people WHERE URL = :MyURL
  UNION ALL
  SELECT 'GZ' AS MySite2, COUNT(Taxon) AS num FROM gz_life WHERE Taxon = :MyURL";

$stmt = $pdo->prepare($sql);
$stmt->bindParam(':MyURL',$MyURL,PDO::PARAM_STR);
$MySite2 = array();
$stmt->execute();
while ($row = $stmt->fetch())
{
 $MySite2[] = array("mysite" => $row['MySite2'], "num" => $row['num']);
}

You can then easily sum or otherwise calculate whatever/however you want 然后,您可以轻松求和或以其他方式计算所需的任何/任何

Perhaps you should change "$MySite2 = $row['MySite2'];" 也许您应该更改“ $ MySite2 = $ row ['MySite2'];” to "$MySite2 = $row['X'];" 到“ $ MySite2 = $ row ['X'];”

Since the entire query is returned as a column named 'X'. 由于整个查询作为名为“ X”的列返回。

The only other column being returned is 'num', 返回的唯一其他列是“ num”,

$row['MySite2'] will not yield a value since no column named 'MySite2' is returned. $ row ['MySite2']不会产生值,因为没有返回名为'MySite2'的列。

Hope this helps. 希望这可以帮助。

Wow, sorry to be so dense, but it isn't completely my fault; 哇,很抱歉这么密集,但这不是我的错。 it appears that PDO and COUNT queries just don't go well together. 看来PDO和COUNT查询不能很好地结合在一起。 So until my programming skills are a little sharper, I think I need to just stick with a plain vanilla COUNT query that displays pages, then make a second "traditional" query that's designed only to give various database tables static values (website and section). 因此,在我的编程技巧变得更精通之前,我想我需要坚持使用显示页面的普通COUNT查询,然后进行第二个“传统”查询,该查询旨在提供各种数据库表静态值(网站和部分) 。 Then, I can move on to more advanced tables that extract details from my DB. 然后,我可以继续使用更高级的表来从数据库中提取详细信息。

Mods: I don't know if I should post this as an answer when it really isn't; Mods:我不知道是否真的应该发布这个答案。 it's more of a workaround. 这更多是一种解决方法。 So feel free to change this from "Answer" to whatever. 因此,随时将其从“答案”更改为任何内容。 Or if it's easier for me to do it, let me know, and I'll delete it and post it as a comment, etc. 或者,如果对我来说更容易些,请告诉我,然后我将其删除并作为评论发布,等等。

There are lots of great tips on this page, and I'll bookmark it as a reference. 此页面上有很多很棒的技巧,我将其标记为参考。 ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM