2

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:

NULL string(1) "5" 

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:

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

**OUTPUT2** is the desired result. 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. I tried **CODE1** just for reducing the length of my code but it isn't working. Why can't we use more than one mysql_fetch_array() like I did in **CODE1** ?

1
  • 2
    next time please consider using code formatting tags Commented Aug 25, 2010 at 10:29

4 Answers 4

3

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)))
Sign up to request clarification or add additional context in comments.

Comments

2

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!

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 !

1 Comment

Thanks for your quick response, although it is not the correct answer that I am looking for. But your point "unless there Exactly the same amount of rows you would run into problems!" is worth noting.
0

mysql_fetch_array() processes DB results row by row. if you do more than mysql_fetch_array in one loop, this means you only walk N row.

where N is the minimum number of rows selected by your '$result's.

2 Comments

Please elaborate your answer its a bit confusing. Thanks for your response.
say you have 10 rows in $result1, 15 rows in result2. doing while($row=mysql_fetch_array($result1) && $row=mysql_fetch_array($result2)) will only process the first 10 rows of the 2 results.
0

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.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.