开发者

PHP foreach loop using a MySQL ID

开发者 https://www.devze.com 2023-03-20 16:21 出处:网络
Is it possible to use a foreach loop for a specific ID in MySQL? This is what I\'m trying to work to (specifically the part: foreach ($row[\"cid\"] )

Is it possible to use a foreach loop for a specific ID in MySQL?

This is what I'm trying to work to (specifically the part: foreach ($row["cid"] )

foreach ($row["cid"] as $value) {

if开发者_如何转开发 ($row["v"] == 'x')
{
    echo $row["n"];     
    break;                  
}


foreach() requires an array as the first parameter. In your example, $row["c_id"] is not an array, so the statement fails.

Instead, you can use a while loop to process each returned row:

while( $row = mysql_fetch_assoc( $query ) ) {
    if ( $row['value'] == 'Yes' ) {
        echo $row['c_name'];
        break;
    }
}


You should use

while($row = mysql_fetch_assoc($query)) { $value = $row['cid']; ... }

But can't understand why do you need that $value if you don't use it.

0

精彩评论

暂无评论...
验证码 换一张
取 消