【问题标题】:Using an SQL result in a foreach loop在 foreach 循环中使用 SQL 结果
【发布时间】:2011-12-16 18:27:23
【问题描述】:

我觉得我在这里遗漏了一些非常明显的东西,我正在尝试获取 SQL 查询的结果,然后在循环中使用它们。我觉得我错过了一些非常明显的东西,我已经尝试过有和没有注释掉的行。

<?php
$sentToID = $_SESSION['userID'];

$query = "SELECT *
          FROM messages
          WHERE sentToID = '$sentToID'";

$results = mysql_query($query);
//$userData = mysql_fetch_array($results, MYSQL_ASSOC);

foreach ($results as $result){
    $messageID = $result['messageID'];
    $sentFromID = $result['sentFromID'];
    $subject = $result['subject'];
    $body = $result['body'];
    $dateTime = $result['dateTime'];

    $query = "SELECT usertype 
              FROM user
              WHERE userID = '$sentFromID'";
    $messageResult = mysql_query($query);
    $messageData = mysql_fetch_array($messageResult, MYSQL_ASSOC);

    $usertype = $messageData['usertype'];

    $query = "SELECT * 
              FROM $usertype
              WHERE userID = '$sentFromID'";

    $messageResult = mysql_query($query);
    $messageData = mysql_fetch_array($messageResult, MYSQL_ASSOC);

    if ($usertype == "jobseeker"){
        $forname = $messageData['forename'];
        $surname = $messageData['surname'];
        echo "<div><p>" . $forename . " " . $surname . "</p>
              <p>Subject: " . $subject ."</p>
              <p>Body: " . $body . "</p></div>";
    }
    if ($usertype == "employer"){
        $forname = $messageData['forename'];
        $surname = $messageData['surname'];
        $companyName = $messageData['companyName'];

        echo "<div><p>" . $forename . " " . $surname . " - " . $companyName . "</p>
              <p>Subject: " . $subject ."</p>
              <p>Body: " . $body . "</p></div>";
    }
}
?>

任何帮助将不胜感激

【问题讨论】:

  • 还有什么问题?
  • 取消注释时,foreach 不应该读为foreach($userData as $result)吗?
  • 如果您不告诉我们问题所在,我们将无法帮助您。无论如何,要使用数组,您需要使用数组,而不是查询!意思是,您的foreach 中有$result 而不是$userData

标签: php mysql foreach


【解决方案1】:

您必须首先将结果提取到数组中。看起来您开始执行此操作但已将其注释掉。

$results = mysql_query($query);
//$userData = mysql_fetch_array($results, MYSQL_ASSOC);

$resultset = array();
while ($row = mysql_fetch_array($results)) {
  $resultset[] = $row;
}

// $resultset now holds all rows from the first query.
foreach ($resultset as $result){
 //... etc...

【讨论】:

    【解决方案2】:

    您应该这样做而不是您的foreach()(请参阅mysql_query() manual page 了解更多信息):

    while($result = mysql_fetch_assoc($results)) {
        // your code
    }
    

    【讨论】:

      【解决方案3】:

      另一种选择

      $num_rows = mysql_num_rows($result)
      for ($i=0;$i<$num_rows;$i++) {
      $row = mysql_fetch_assoc($result)
      $messageID = $row['messageID'];
      }
      

      你可以从这里做任何事情。

      记住, 1- 查询像 $result 这样的对象 2- 一次从对象中获取行到一个数组中,该数组反映了表中的整行,给定您的查询定义与关联键或数字 3- 对数组做一些事情

      您将逐行遍历对象并将 $row 作为数组放入。

      干杯

      【讨论】:

        【解决方案4】:

        我没有评论的声誉,上面的答案是正确的,但是php mysql_query() manual page

        此扩展在 PHP 5.5.0 中被弃用,并在 PHP 中被删除 7.0.0。相反,应该使用 MySQLi 或 PDO_MySQL 扩展

        所以现在正确的方法是:

        while($result = mysqli_fetch_assoc($results)) {
            // your code
        }
        

        【讨论】:

          【解决方案5】:

          $results = mysql_query($query); 的返回类型是对象类型的资源。例如:

          object(mysqli_result)#3 (5) { 
            ["current_field"] => int(0) 
            ["field_count"] => int(3) 
            ["lengths"] => NULL 
            ["num_rows"] => int(2) 
            ["type"] => int(0) 
          }
          

          因为,我们只对表格数据感兴趣。我们将资源传递给mysql_fetch_array(),以及其他处理结果表的函数,根据PHP Docs访问返回的数据

          我们不能直接使用它。

          但是,如果您在理解 while 循环的内部工作时遇到问题,我会给您一个示例。

          <?php
          
              $s = 0;
          
              class deepe {
                  function dis(){
                      $a = array(2,3,4,5,6);
                      $b = $GLOBALS['s'];
                      if ( $b < count($a) )
                          return $a[$b];
                  }
              }
          
              $chk = new deepe();
          
              while($fac = $chk->dis())
              {
                  echo $fac."<br />";
                  $GLOBALS['s']++;
              }
          
          ?>
          

          【讨论】:

            猜你喜欢
            • 2013-04-26
            • 2012-01-22
            • 2014-12-05
            • 2017-12-20
            • 1970-01-01
            • 2015-05-30
            • 1970-01-01
            • 1970-01-01
            • 2017-03-10
            相关资源
            最近更新 更多