在这个网站的帮助下,我现在有一个显示来自多个数据库表的页面的查询,其中$ MyURL等于页面URL(例如,MySite / Crazy_Horse匹配表人员中的Crazy_Horse值,字段URL)。
唯一剩下的问题是静态值(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;
}
}
答案 0 :(得分:0)
以这种方式更改您的查询:
$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。
告诉我它是否正常