doujie7497 2014-05-27 02:09
浏览 13
已采纳

为什么我的查询没有定义这个值?

I have a database query that displays a list of national birds. I picked up a script that adds a cool detail: The ability to add asterisks, stars, etc. to birds that represent more than one nation.

However, I must have changed something in my code or database table that broke it. I now get the error message Undefined index: Stars. Can anyone spot the problem?

I pasted the query into phpMyAdmin > SQL, and it seems to work OK, displaying rows representing nations with national birds...

SELECT GS.N, GS.IDArea SymArea, GS.IDSymbol, GS.URL, GS.Title,
GS.PageKind3, GS.Symbol, GS.Latin, GS.Desig, GS.DesigGen, GS.DesigGroup, GS.Date,
GG.N, GG.IDArea, GG.Name, GG.Type, GG.IDParent, GG.IDParent2, GG.Parent, GG.Parent2,    GG.IDReg, 
T.Stars 
FROM gs AS GS 
LEFT JOIN gw_geog AS GG ON GG.IDArea = GS.IDArea 

LEFT JOIN ( 
 SELECT Latin, COUNT(Latin) as Stars 
 FROM gs 
 GROUP BY Latin
) as T ON GS.Latin = T.Latin 

WHERE GS.DesigGen = 'bird' AND GG.Type = 'nat' AND GS.Symbol != '' 
OR GS.DesigGen = 'bird' AND GG.Type = 'dep' AND GS.Symbol != '' 
GROUP BY GS.IDArea ORDER BY GS.N

The most important value is probably GS.Latin, which simply lists various birds' Latin, or scientific names. It appears that the above query isn't "connecting" with the script in my loop, below. Does anyone have a hunch what the problem is?

while ($row = $stm->fetch())
{

  switch (TRUE)
 { 
  case ($row['Stars'] == 2): 
  $star_rating = ' (2)'; 
  break; 
  case ($row['Stars'] == 3): 
  $star_rating = ' (3)'; 
  break; 
  case ($row['Stars'] == 4): 
  $star_rating = ' (4)'; 
  break; 
  case ($row['Stars'] > 5): 
  $star_rating = ' (5 or more)'; 
  break; 
  default: 
  $star_rating = ''; 
  break; 
 }

}
  • 写回答

2条回答 默认 最新

  • douzhan1238 2014-05-27 02:27
    关注

    I think your left join make the Stars field to be null try to replace T.Stars in the query fields to

    COALESCE(T.Stars,0) AS `Stars`
    

    This will set a default 0 if the field is null

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程