array_combine用法或查询错误?

时间:2012-03-25 03:43:08

标签: php mysql arrays

我之前从未使用过array_combine,而且我在查询中遇到“Boolean而不是resource”错误。如果我将查询更改为读取... WHERE cal_id =“。$ quidx。”;布尔错误消失但我在脚本中稍后出现意外的T_STRING错误,原因我无法理解,查询结果也没有。

主要问题:这是array_combine的正确用法吗?如果是这样,我在此脚本的其余部分中缺少什么,在查询后没有信息? $ quid1是一个id数组,$ tm是一个Unix时间戳数组。两个阵列都检查出一致的行数。在array_combine之后,$ cls1返回两个先前数组的有效数组,但它们似乎在查询中不起作用。

感谢您的帮助。我还在学习。

我编辑了脚本以包含新的查询语句。现在的问题是foreach语句,其中错误读取无效参数。

$cls1 = array_combine($quid1, $tm);
      $quidx = array_values($quid1);
      $quclx = array_values($tm);
//// note to self.. start final query for email write with new id data, likely redundant.
$qumail = "SELECT cal_id, cal_name, cal_time, cal_description FROM webcal_entry WHERE cal_id in (" . implode (',' , $quidx) . ")";
$wemail = mysql_query($qumail);
while ($row = mysql_fetch_array($wemail, MYSQL_NUM)) {
    $quname2 = $row[1];
    $qtime = $row[2]
    $qudesc2 = $row[3];

以下是适用的完整脚本部分。我还在啄它所以它还没有修好:

$cls1 = array_combine($quid1, $tm);
      $quidx = array_values($quid1);
      $quclx = array_values($tm);
//// note to self.. start final query for email write with new id data, likely redundant.
$qumail = "SELECT cal_id, cal_name, cal_time, cal_description FROM webcal_entry WHERE cal_id in (" . implode (',' , $quidx) . ")";
$wemail = mysql_query($qumail);
while ($row = mysql_fetch_array($wemail, MYSQL_NUM)) {
    $quname2 = $row[1];
    $qtime = $row[2]
    $qudesc2 = $row[3];
}
    foreach ($qtime as $key=>$btUx) {
        if (strlen($btUx) < 6){
        $btUx = '0' . $btUx;
        date_default_timezone_set('UTC');
        $unixEpoch = strtotime($btUx);
        date_default_timezone_set('America/Denver');
        $formtime = date("H:i", $unixEpoch);
      }
    }
    foreach ($tm as $key=>$tf) {
        $idnotime = 0;
        $idnow = (strlen($tf) > 2);

    switch($tf) {
      case $idnow:
        $repmlnow = sprintf("Event: %s \nTime: %s \nDesc: %s \n\n", $row[1], $formtime, $row[3]);
        break;
      case $idnotime:
        $repmlnotm = sprintf("Event: %s \nDesc: %s \n\n", $row[1], $row[3]);
        break;
   }
}   
/////===================== send mail... 

除非有其他评论,否则这是我的上一次编辑。我已将原始脚本更改为以下内容,在这种情况下,由于下面的贡献者,所有内容似乎都能正常工作...

$cls1 = array_combine($quid1, $tm);
      $quidx = array_values($quid1);
      $quclx = array_values($tm);
$qumail = "SELECT cal_id, cal_name, cal_time, cal_description FROM webcal_entry WHERE cal_id in (" . implode (',' , $quidx) . ")";
$wemail = mysql_query($qumail);
while ($row = mysql_fetch_array($wemail, MYSQL_NUM)) {
    $quname2 = $row[1];
    $qtime = $row[2];
    $qudesc2 = $row[3];
}
if(strlen($qtime) < 6){
    $btUx = '0' . $qtime;
    date_default_timezone_set('UTC');
    $unixEpoch = strtotime($btUx);
    date_default_timezone_set('America/Denver');
    $formtime = date("H:i", $unixEpoch);
}elseif(strlen($qtime) > 5){
    date_default_timezone_set('UTC');
    $unixEpoch = strtotime($value);
    date_default_timezone_set('America/Denver');
    $formtime = date("H:i", $unixEpoch);
}
foreach ($quclx as $key=>$tf) {
    $idnotime = 0;
    $idnow = (strlen($tf) > 2);

    switch($tf) {
      case $idnow:
        $repmlnow = sprintf("Event: %s \nTime: %s \nDesc: %s \n\n", $quname2, $formtime, $qudesc2);
        break;
      case $idnotime:
        $repmlnotm = sprintf("Event: %s \nDesc: %s \n\n", $quname2, $qudesc2);
        break;
  }
}   
/////===================== send mail...  

1 个答案:

答案 0 :(得分:1)

$ quidx是一个数组索引作为键的数组,$ quid1数组值作为值。尝试更改查询成为:

$qumail = "SELECT cal_id, cal_name, cal_time, cal_description FROM webcal_entry WHERE cal_id in (" . implode (',' , $quidx) . ")";