Mysql
 sql >> Database >  >> RDS >> Mysql

while($riga =mysql_fetch_assoc($risultato)) - Come foreach $riga?

Diciamo che ciascuna delle righe del tuo database è simile a questa...

[product_id][product_name][product_description][product_price]

Quando assegni la tua query, ritorna a una variabile passata tramite mysql_fetch_assoc() usando un ciclo while, ogni passaggio isolerà un'intera riga. Di cui puoi separare manualmente per riferimento alla chiave dell'array ($array['product_id'] ) o utilizzando un ciclo foreach. Penso che il problema che stai riscontrando sia che stai confondendo questo. Tenendo presente il layout della tabella di esempio sopra, potresti fare qualcosa di simile al seguente:

while ($tableRow = mysql_fetch_assoc($query)) { // Loops 3 times if there are 3 returned rows... etc

    foreach ($tableRow as $key => $value) { // Loops 4 times because there are 4 columns
        echo $value;
        echo $tableRow[$key]; // Same output as previous line
    }
    echo $tableRow['product_id']; // Echos 3 times each row's product_id value
}

Guarda questa riga nel tuo codice:if ($product['id'] == $id) { }

Penso che probabilmente intendi if ($row['id'] == $id) { } invece.