简体   繁体   中英

While Loop with Count Query? (PHP/MySQL)

How can I use a while loop with a COUNT query? In the series of subqueries below, I assigned each table a static value (eg '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...

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. The other possibility is that while loops don't work with COUNT queries. Either way, can someone tell me what the solution is?

i dont think your query return the column 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'];" to "$MySite2 = $row['X'];"

Since the entire query is returned as a column named 'X'.

The only other column being returned is 'num',

$row['MySite2'] will not yield a value since no column named 'MySite2' is returned.

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. 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). 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; 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. ;)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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