继续将Array转换为字符串转换通知,但无法理解原因

时间:2013-01-30 21:55:52

标签: php mysql

我一直在获取一个数组到字符串转换的通知,它似乎来自我已经转义(或没有转义)MySQL语句的方式。如果我不使用PHP变量并将数据作为文本放入它可以工作,但是使用变量它会显示错误。救命! :)

function lastBackupStatus($server,$node){
  $serverstatus = "";
  $i=1;
  $result = mysql_query("select
    max(case when status LIKE '%Failed%' then scheduled_start end) Last_Failed,
    max(case when status LIKE '%Missed%' then scheduled_start end) Last_Missed,
    max(case when status LIKE '%Completed%' then scheduled_start end) Last_Completed,
    node_name,
    schedule_name
    from `events_".$server."`
    where node_name='".$node."'
    group by node_name, schedule_name
    ORDER BY last_failed");
  $count=mysql_num_rows($result);
  if ($count!== 0){
    while ($info = mysql_fetch_assoc( $result )){
      if($i < $count){
        $serverstatus .="";//do nothing
      }else{
        if($info['Last_Failed']  > $info['Last_Completed']){
          if($info['Last_Failed'] > $info['Last_Missed']){
            $serverstatus .="Failed:". $info['Last_Failed'].
              $info['node_name'].$info['schedule_name']."</br>";
          }
        }
        if($info['Last_Missed'] > $info['Last_Completed']){
          if ($info['Last_Missed'] > $info['Last_Failed']){
            $serverstatus .=" Missed: ". $info['Last_Failed'].
              $info['node_name'].$info['schedule_name']."</br>";
          }
        }
      }
      $i++;
    }
  } else {
    $serverstatus .= "count is 0";
  }
  return $serverstatus;
}

1 个答案:

答案 0 :(得分:0)

您忘记了AS关键字:

max(case when status LIKE '%Failed%' then scheduled_start end) Last_Failed, 
max(case when status LIKE '%Missed%' then scheduled_start end) Last_Missed,
max(case when status LIKE '%Completed%' then scheduled_start end) Last_Completed,

应该是

max(case when status LIKE '%Failed%' then scheduled_start end) AS Last_Failed, 
max(case when status LIKE '%Missed%' then scheduled_start end) AS Last_Missed,
max(case when status LIKE '%Completed%' then scheduled_start end) AS Last_Completed,

因此$info['Last_Failed']没有关联索引等等。

根据你的评论,看来$node是一个数组,你需要替换它:

where node_name='".$node."'

用这个:

where node_name='".$node['node_name']."'