在此站点的大量帮助下,我现在有一个查询来显示来自多个数据库表的页面,其中$ MyURL等于页面URL(例如MySite / Crazy_Horse与表人员字段URL中的Crazy_Horse值匹配)。

唯一剩下的问题是静态值(MySite2)。我无法在显示页面上回显它。但是它一定能正常工作,因为如果我注释掉这一行-$ MySite2 = $ row ['MySite2']; -我收到一条错误消息,提示尚未定义$ MySite2。但是,当我恢复该行时,错误消息消失了,但是echo $ MySite2没有显示任何内容。

谁能看到我在做什么错?

$sql = "SELECT SUM(num) as num FROM (
  SELECT 'GZ' AS MySite2, COUNT(Taxon) AS num FROM gz_life WHERE Taxon = :MyURL
  UNION ALL
  SELECT 'All' AS MySite2, COUNT(Name) AS num FROM gw_geog WHERE Name = :MyURL
  UNION ALL
  SELECT 'GS' AS MySite2, COUNT(URL) AS num FROM gs WHERE URL = :MyURL
 ) AS X";

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

while ($row = $stmt->fetch())
{
 $MySite2 = $row['MySite2'];
 $Total = $row['num'];
 switch($Total)
{
 case 1:
 require($BaseINC."/$MyPHP/inc/C/2_Child.php");
 break;
 case 0:
 require_once($BaseINC."/404.php");
 break;
 default:
 require($_SERVER['DOCUMENT_ROOT']."/Dupe.php");
 break;
  }
}

最佳答案

以这种方式更改查询:

$sql = "SELECT X.MySite2, SUM(x.num) as num FROM (
  SELECT 'GZ' AS MySite2, COUNT(Taxon) AS num FROM gz_life WHERE Taxon = :MyURL
  UNION ALL
  SELECT 'All' AS MySite2, COUNT(Name) AS num FROM gw_geog WHERE Name = :MyURL
  UNION ALL
  SELECT 'GS' AS MySite2, COUNT(URL) AS num FROM gs WHERE URL = :MyURL
 ) AS X group by X.MySite2"


如果要显示MySite2,则必须围绕该值对聚合函数进行分组。

您无法显示,因为查询的可见性已由较高级别提供,因此我在您的字段列表中添加了MySite2。

告诉我是否可以

10-08 17:40