PHP foreach() - 来自MySQLi查询的无效参数?

时间:2017-02-09 07:34:54

标签: php arrays

我正在努力使用mysqli查询数组。我正在查询数据库中的日志案例引用号,需要检查是否有任何引用具有包含任何文件的相同引用号的文件夹。如果是,我必须scandir文件夹内容并通过$mail->AddAttachment()将每个文件附加到邮件中。

这是一个简单的代码,我在网站的其他部分使用它很好,但是在尝试读取数组时会抛出invalid argument错误。

PHP: -

// query records submitted within the last 24 hours
$sql = "SELECT LogID FROM qci_dmlog_data GROUP BY LogID";
$result = $mysqli->query($sql) or die('<p>Query to get data from qci_dmlog_data table failed: ' . mysqli_error($mysqli) . '</p>');

// define path to attachment folder
$log_folder = "../attachments/";


while ($row = $result->fetch_array(MYSQLI_ASSOC)) {

print($row['LogID']);

    foreach ($row['LogID'] as $case) {

      // check if folder with case reference exists or not
      if (file_exists($log_folder.$case)) {
        $files = scandir($log_folder.$case);
        $n = 1;

        // ignore . and .. folders
        foreach($files as $file) {      
          if (in_array($file, array(".",".."))) continue;

          // get file extension and rename filename only
          $extension = pathinfo($file, PATHINFO_EXTENSION);
          $newName = str_replace($file,$n.'.'.$extension,$file);
          rename($log_folder.$case."/".$file, $log_folder.$case."/".$newName);

          // attach to email
          $mail->addAttachment($log_folder.$case."/".$newName);
          echo "Attachment: $newName<br>";
          $n++;
        }
      }
    }
}

请问我在这里做错了什么?

由于

2 个答案:

答案 0 :(得分:1)

由于$ row [&#39; LogID&#39;]中只有一个值,因此参数无效。使用foreach循环,您可以逐个元素地遍历数组。因为你在while循环中,获取每一个LogID,所以你可以删除foreach,因为每次循环while循环只能获得一个元素。

这应该适合你:

// query records submitted within the last 24 hours
$sql = "SELECT LogID FROM qci_dmlog_data GROUP BY LogID";
$result = $mysqli->query($sql) or die('<p>Query to get data from qci_dmlog_data table failed: ' . mysqli_error($mysqli) . '</p>');

// define path to attachment folder
$log_folder = "../attachments/";


while ($row = $result->fetch_array(MYSQLI_ASSOC)) {

print($row['LogID']);

$case = $row['LogID'];

// check if folder with case reference exists or not
if (file_exists($log_folder.$case)) {
    $files = scandir($log_folder.$case);
    $n = 1;

    // ignore . and .. folders
    foreach($files as $file) {      //IS FILES AN ARRAY?
      if (in_array($file, array(".",".."))) continue;

      // get file extension and rename filename only
      $extension = pathinfo($file, PATHINFO_EXTENSION);
      $newName = str_replace($file,$n.'.'.$extension,$file);
      rename($log_folder.$case."/".$file, $log_folder.$case."/".$newName);

      // attach to email
      $mail->addAttachment($log_folder.$case."/".$newName);
      echo "Attachment: $newName<br>";
      $n++;      
  }
}

正确使用foreach:

foreach($array as $arrayElement) {

}

答案 1 :(得分:1)

我假设$row['LogID']不是数组,它是一个字符串。因此,您需要从代码中删除第一个foreach(),如下所示: -

while ($row = $result->fetch_array(MYSQLI_ASSOC)) {

$case = $row['LogID']);

      // check if folder with case reference exists or not
      if (file_exists($log_folder.$case)) {
        $files = scandir($log_folder.$case);
        $n = 1;

        // ignore . and .. folders
        foreach($files as $file) {      
          if (in_array($file, array(".",".."))) continue;

          // get file extension and rename filename only
          $extension = pathinfo($file, PATHINFO_EXTENSION);
          $newName = str_replace($file,$n.'.'.$extension,$file);
          rename($log_folder.$case."/".$file, $log_folder.$case."/".$newName);

          // attach to email
          $mail->addAttachment($log_folder.$case."/".$newName);
          echo "Attachment: $newName<br>";
          $n++;
        }
      }
}

注意: - 检查所有其他变量,例如$log_folder已经设置并且具有正确的值。谢谢