无法从FOREACH循环中正确显示结果

I switched my code to PDO and I almost there, except for this part where all results from a query are supposed to be displayed with a foreach statement. I know the data is being fetched properly and is correctly stored in $row2.

What I need to do is get take the 'position' variable from $results2 and add 1 to it and then run the foreach loops where position is equal to that new number...

BEFORE I SWITCHED TO PDO, my code was working perfectly and this did correctly display the 2 expected results:

$cont = $results2[0]->position;;
$cont++;

foreach ($results2 as $resulting) {
if ( $results2[0]->position = $cont )
{
echo "<hr><br><br>" . $resulting->text . "&nbsp;" . "   <b>Suggested by: $resulting-
>display_name </b><br>|<b> <a href=\"$url\">Approve this contribution</a></b> | <b><a
href=\"$url\">delete this suggestion</a></b> |";
}
}

But now, the foreach only returns the same first result twice after the conversion to PDO, which looks like this:

//NEW PDO CODE--DOESN'T DISPLAY BOTH RESULTS:

$results2->execute();
$row2 = $results2->fetchAll(PDO::FETCH_ASSOC);

$cont = $row2[0]['position'];
$cont++;

foreach ($row2 as $resulting) {
if ( $row2[0]['position'] == $cont )
{
echo "<hr><br><br>" . $row2[0][text] . "&nbsp;" . "   <b>Suggested by:" . $row2[0]
[display_name] . "</b><br>|<b> <a href=\"$url\">Approve this contribution</a></b> | <b>
<a href=\"$url/?WID=$resulting->WID\">delete this suggestion</a></b> |";
}
}

It looks like your old code was fetching an array of objects, whereas your new code is fetching an array of arrays (indicated by PDO::FETCH_ASSOC).

So while $results2[0]->position used to work, you mistakenly replaced it with $row2[0][position]. However, position needs to be quoted in the second case since it is an array index.

If you had all of your error reporting turned on, you would have easily seen this.

The same applies to $row2[0][text] and $row2[0][display_name]. In addition to the lack of quotes on these indexes, you probably really want $resulting instead of $row[0] since $row[0] will give you the same values each time through the loop. So those should probably be changed to $resulting['text'] and $resulting['display_name']

So replace all instances of $row2[0][position] with $row2[0]['position']. Also, heed the advice of John Ruddell.

NOTE: this is not an answer... its just a way for me to clarify what I was saying in comments because its hard to show code in comments.

GENERAL IF CODE LOGIC:

if (true){
    // code will always execute on true
}

if (false){
    // code will never execute on false
}

sample:

$key = "12345";
if($key){
    // will always be executed because this is a true statement. 
}

your code:

if ( $row2[0]['position'] = $cont ){
    // will always execute because the assignment is true
}

NOW: with all that aside.. if nothing is happening when you run it with == that means that the count is not equal to the $row[0][position] so its a false statement and the code inside the if does not get executed.

RECOMMENDATION:

try putting an echo $cont; and echo $row2[0]['position']; before the if statement to see whats going on.