-5

構文に関してこのコードに何か問題があります

    $i=0;
while ($r=$sth->fetch(PDO::FETCH_ASSOC))||($a=$sth_temp->fetch(PDO::FETCH_ASSOC)) {
  $i++;
  $itemcount=countitemsofuser($r['id']);
  echo "\n<tr>";
  echo "<td><a class='editid' href='$scriptname?action=editsoftware&amp;id=".$r['id']."'>{$r['id']}</a></td>\n";
  echo "<td>{$r['manufacturerid']}</td>\n";
  echo "<td>{$r['stitle']}</td>\n";
  echo "<td>{$r['sversion']}</td>\n";
  echo "<td>{$a['stype']}</td>\n";

ご協力ありがとうございました。

4

1 に答える 1

2

you have one closing ) too many:

while ($r=$sth->fetch(PDO::FETCH_ASSOC))||($a=$sth_temp->fetch(PDO::FETCH_ASSOC)) {
                                   //here

It should be:

while ($r=$sth->fetch(PDO::FETCH_ASSOC)||($a=$sth_temp->fetch(PDO::FETCH_ASSOC)) {

Even so, this code isn't quite what I'd call good code. You're fetching data from either one of two statements, not both satements at the same time... never both, in fact, owing to PHP short-circuiting your while condition. Lets replace while with if:

if ($r = $sth->fetch(PDO::FETCH_ASSOC) || $a = $sth_temp->fetch(PDO::FETCH_ASSOC))
    //if fetched, $r is truthy, and this branch will be executed
   //    the second expression (right of the || operator) need never be evaluated

because of short-circuit evaluation, PHP will not execute the second expression ($a = ...) when the first condition evaluates to true.
If the first fetch call is truthy, the while-loop condition is true, simply because that's what or means: if this or that is true. There's no point in evaluating the second expression, the result of the condition is a given: it's true.

So, essentially, the loop will run through all result-sets of $sth->fetch(), and once there aren't any left to fetch there, that's when $sth_temp->fetch will be called. You could've written this:

while($r = $sth->fetch(PDO::FETCH_ASSOC))
{
    //process $r
}
while(false || $a = $sth->fetch(PDO::FETCH_ASSOC))
{//the false is what $sth->fetch() would be after a while in your code
    //process $a
}

Not what you want, is it. Even so: because you used the || operator, you acknowledge the possibility that, in some cases either one of the 2 fetch calls you're executing might fail, yet inside the while-loop, you simply assume for both $a and $r to be assigned associative arrays:

$itemcount=countitemsofuser($r['id']);
//...
echo "<td>{$a['stype']}</td>\n";

That's just all shades of wrong. If you want to process the data at the same time, either use &&:

while($r = $sth->fetch(PDO::FETCH_ASSOC) && $a = $sth_temp->fetch(PDO::FETCH_ASSOC))
{
}

To avoid the problems with short-circuit evaluation. Still, the moment one of these fetch calls returns false, you'll have to process the remainder of the data like this:

$leftRow = $a ? $a : $r;//get the fetched data from the resultset that isn't empty
$left = ($a ? $sth_temp : ($r ? $sth : false));//get the object on which to fetch
if ($left)
{//if there are some left:
    do
    {//begin with row that was already fetched
        //process $leftRow
    } while($leftRow = $left->fetch(PDO::FETCH_ASSOC));//and keep fetching
}

But that's just awful, isn't it.
It's far more likely that you can solve this problem easily and much more efficiently by altering your query, to use a JOIN for example...

于 2013-10-24T10:50:45.217 に答える