在1循环中取两个数组(不工作)

时间:2015-10-28 08:59:59

标签: php mysql

我试图从两个查询中获取数组。结果只获得第一个索引。

while(($row2 = mysql_fetch_array($query1)) && ($index = mysql_fetch_array($query2)))
{
   $leaveID = $row2['leaveID'];
   $personID = $index['personID'];
   $Person_Type = $index['Person_Type'];

   if ($Person_Type == 'Regular') 
   {
      $Sick_Remaining_Days = 10 - $Sick_Total_Days;
   }else{
      $Sick_Remaining_Days = 5 - $Sick_Total_Days;
   }    

   echo "<tr>";
   echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
   echo "</tr>"; 
}

5 个答案:

答案 0 :(得分:0)

假设两个查询都没问题,我会根据您的需要提供两种方法来循环查询。希望他们中的一个会有所帮助。

<?php
$arrayRowA = array();
$arrayRowB = array();
while($row = mysql_fetch_array($query1)){$arrayRowA[] = $row;}
while($row = mysql_fetch_array($query2)){$arrayRowB[] = $row;}


// Loop through two arrays in a square way (every combination of both arrays)
foreach($arrayRowA as $keyA => $objectA){
    foreach($arrayRowB as $keyB => $objectB){
        $leaveID        = $objectA['leaveID'];
        $personID       = $objectB['personID'];
        $Person_Type    = $objectB['Person_Type'];

        if($Person_Type == 'Regular') 
        {
            $Sick_Remaining_Days = 10 - $Sick_Total_Days;
        }else{
            $Sick_Remaining_Days = 5 - $Sick_Total_Days;
        }    

        echo "<tr>";
        echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
        echo "</tr>"; 
    }
}


// Loop through two arrays in a linear way (one to one)
foreach($arrayRowA as $keyA => $objectA){
    if(isset($arrayRowB[$keyA])){
        $objectB        = $arrayRowB[$keyA];
        $leaveID        = $objectA['leaveID'];
        $personID       = $objectB['personID'];
        $Person_Type    = $objectB['Person_Type'];

        if($Person_Type == 'Regular') 
        {
            $Sick_Remaining_Days = 10 - $Sick_Total_Days;
        }else{
            $Sick_Remaining_Days = 5 - $Sick_Total_Days;
        }    

        echo "<tr>";
        echo ($leaveID == $personID) ? "<td>$Sick_Remaining_Days</td>" : "<td>--</td>";
        echo "</tr>"; 
    }
}
?>

答案 1 :(得分:0)

$query = mysql_query("select p*, l* from person as p left join leave as l on p.personID = l.leaveID");
while(($row = mysql_fetch_array($query)))
{
   $Person_Type = $row['Person_Type'];
   if ($Person_Type == 'Regular') 
   {
      $Sick_Remaining_Days = 10 - $Sick_Total_Days;
   }else{
      $Sick_Remaining_Days = 5 - $Sick_Total_Days;
   }    

   echo "<tr>";
   echo "<td>$Sick_Remaining_Days</td>";
   echo "</tr>"; 
}

答案 2 :(得分:0)

你应该在之前获得数组:

<?php
/**
 * PHPMailer simple file upload and send example
 */
$msg = '';
if (array_key_exists('userfile', $_FILES)) {
    // First handle the upload
    // Don't trust provided filename - same goes for MIME types
    // See http://php.net/manual/en/features.file-upload.php#114004 for more thorough upload validation
    $uploadfile = tempnam(sys_get_temp_dir(), sha1($_FILES['userfile']['name']));
    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
        // Upload handled successfully
        // Now create a message
        // This should be somewhere in your include_path
        require 'PHPMailerAutoload.php';
        $mail = new PHPMailer;
        $mail->setFrom('from@example.com', 'First Last');
        $mail->addAddress('whoto@example.com', 'John Doe');
        $mail->Subject = 'PHPMailer file sender';
        $mail->msgHTML("My message body");
        // Attach the uploaded file
        $mail->addAttachment($uploadfile, 'My uploaded file');
        if (!$mail->send()) {
            $msg = "Mailer Error: " . $mail->ErrorInfo;
        } else {
            $msg = "Message sent!";
        }
    } else {
        $msg = 'Failed to move file to ' . $uploadfile;
    }
}
?>

现在使用两个阵列。

或尝试使用新的http://php.net/manual/en/mysqli-result.fetch-array.php。可能在使用第二个查询后丢失了与第一个查询的连接。

答案 3 :(得分:0)

while($row = mysqli_fetch_assoc($result))
{
     $row2 = mysqli_fetch_assoc($result2);
       echo $row['id'];
       echo $row2['id'];

/* your code  */
}    `

答案 4 :(得分:-1)

使用此...这就是您要搜索的内容!

while($row2 = mysql_fetch_array($query1))
{
 $index = mysql_fetch_array($query2);
   echo $row['id'];
   echo $index['id'];

/* add code  */
}