简体   繁体   English

多个mysql_fetch_array()

[英]More than one mysql_fetch_array()

CODE1: CODE1:

while( $row1 = mysql_fetch_array($result2) && $row2 = mysql_fetch_array($result4) )
{
 $details[0] = $row1[0];
 $details[1] = $row2[0];
 var_dump($details[0]);
 var_dump($details[1]);
}

OUTPUT1: OUTPUT1:

NULL string(1) "5" 

CODE2: CODE2:

while($row1 = mysql_fetch_array($result2))
{
 $details[0] = $row1[0];
 var_dump($details[0]);
}
while($row2 = mysql_fetch_array($result4))
{
 $details[1] = $row2[0];
 var_dump($details[1]);
}

OUTPUT2: OUTPUT2:

string(6) "728548" string(1) "5"

**OUTPUT2** is the desired result. **OUTPUT2**是理想的结果。 I have checked the rest portion of my code that I haven't mentioned here, nothing is wrong with that. 我已经检查了代码的其余部分,这里没有提到,这没什么不对的。 Using **CODE1** instead of **CODE2** gives wrong result. 使用**CODE1**而不是**CODE2**会产生错误的结果。 I tried **CODE1** just for reducing the length of my code but it isn't working. 我尝试了**CODE1**只是为了减少代码长度,但是它不起作用。 Why can't we use more than one mysql_fetch_array() like I did in **CODE1** ? 为什么我们不能像在**CODE1**那样使用多个mysql_fetch_array()

The operator precedence of && is higher than = . &&运算符优先级高于= So, put parenthesis around the two parts and try this: 因此,在两个部分周围加上括号,然后尝试以下操作:

while(($row1 = mysql_fetch_array($result2)) && ($row2 = mysql_fetch_array($result4)))

Thats like doing 那就像在做

$true = false;

while(true && $true)
{
   $true = false;
}

if anyone of them becmome false / null the other will fail aswell, so unless there Exactly the same amount of rows you would run into problems! 如果它们中的任何一个becmome为false / null,则另一个也会失败,因此,除非存在相同数量的行,否则您将遇到问题!

what your best of doing in a situation like that is 在这种情况下,您最好的做法是

while( ($result_2 = mysql_fetch_array($result2)) || ($result_4 = mysql_fetch_array($result4)))
{
    if($result_2)
    {
        //Do something
    }
    if($result_4)
    {
        //Do something
    }
}

using the || 使用|| / or allows the one to be cancelled out and the other to continue, but this is really not good programming standards ! / or允许其中一个被取消,另一个可以继续,但这确实不是一个好的编程标准!

mysql_fetch_array() processes DB results row by row. mysql_fetch_array()逐行处理数据库结果。 if you do more than mysql_fetch_array in one loop, this means you only walk N row. 如果您在一个循环中执行的操作比mysql_fetch_array多,则意味着您只能走N行。

where N is the minimum number of rows selected by your '$result's. 其中N是“ $结果”选择的最小行数。

The only proper way to reduce code length is to make a function . 减少代码长度的唯一正确方法是制作函数
For example a function which will take a query and it's parameters as arguments and return an array. 例如,一个函数将一个查询及其参数作为参数并返回一个数组。
Like this one 像这个

<?
function dbgetarr(){
  $a = array();
  $query = $args[0];
  unset ($args[0]);
  foreach ($args as $key => $val) {
    $args[$key] = "'".mysql_real_escape_string($val)."'";
  }
  $query = vsprintf($query, $args);

  $res = mysql_query($query);
  if (!$res) {
    trigger_error("dbget: ".mysql_error()." in ".$query);
  } else {
    while($row = mysql_fetch_assoc($res)) $a[]=$row;
  }
  return $a;
}

but it seems you need not array nor while operator in your case. 但是看来您不需要数组和while运算符。
But just as silly code as 但是就像愚蠢的代码一样

$row1 = mysql_fetch_array($result2) 
$row2 = mysql_fetch_array($result4)
$details = array($row1[0],$row2[0]);

so we can learn from this that to understand what are you doing always helps 因此我们可以从中学到,了解您在做什么总是有帮助的

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM