Warning: Invalid argument supplied for foreach()

Fabio Grasso

Nuovo Utente
6 Nov 2014
26
0
0
34
www.grassofabio.com
Buona serata e rieccomi di nuovo quì :)

Devo recuperare dei valori da una tabella.
Se nella tabella ci sono dei valori questi vengono estratti correttamente,mentre se la tabella è vuota nello spazio dove dovrebbero esserci degli utenti viene stampato questo messaggio Warning: Invalid argument supplied for foreach().

Utilizzo XAMPP e questo è il codice che ho usato.
Controller:
PHP:
// includo la connessione al database
include '../../include/db.inc.php';

// recupero gli utenti dal database
try { 
   $result = $pdo->query('SELECT id, cognome, nome, username,password FROM utenti');
}
catch (PDOException $e) {
   $error = 'Errore in recupero utenti dal database';
   include 'error.html.php';
   exit();
}

foreach ($result as $row) {
   $utenti[] = array('id' => $row['id'], 'cognome' => $row['cognome'], 'nome' => $row['nome'], 'username' => $row['username'], 'password' => $row['password']);	
}

include 'utenti.html.php';

Visualizzazione dei risultati
PHP:
<?php foreach($utenti as $utente): ?>
    ...  ...  ...
<?php endforeach; ?>

Qualcuno potrebbe spiegarmi gentilmente perchè esce quel warning e magari come disattivarlo.
 
Ultima modifica di un moderatore:

Longo8

Utente Attivo
28 Mar 2011
1.694
0
36
Prova a fare così:
PHP:
if(is_array($result){
   foreach ($result as $row) {
      $utenti[] = array('id' => $row['id'], 'cognome' => $row['cognome'], 'nome' => $row['nome'], 'username' => $row['username'], 'password' => $row['password']);    
   }
}
 

Fabio Grasso

Nuovo Utente
6 Nov 2014
26
0
0
34
www.grassofabio.com
Allora,provando come mi hai suggerito il warning c'è sempre, mentre inserendo l'if con la funzione is_array prima del foreach ma nella pagina dove visualizzo i risultati,ovvero qua
if(is_array($utenti){
foreach($utenti as $utente):
... ... ...
endforeach;
}
il warning è scomparso.
Grazie mille per l'aiuto :)
 

Longo8

Utente Attivo
28 Mar 2011
1.694
0
36
Ah si vero. Intendevo li ma in realtà ho copiato l'altro codice all'interno dell'if :)
Hai fatto bene.
Figurati.
 
Discussioni simili
Autore Titolo Forum Risposte Data
JackIlPazzo Warning: Invalid argument supplied for foreach() PHP 1
F [MySQL] Warning 1265 data truncated for column... MySQL 5
B Warning: mysql_connect(): Access denied for user 'DIREZIONE'@'81.88.55.179' (using password: YES) PHP 6
G [PHP] Warning: Cannot modify header information - headers already sent by PHP 3
G [PHP] Problema - Warning: session_start() [function.session-start] PHP 9
G Warning: mysql_num_rows() che problema è? PHP 3
P Warning: mysqli_query() expects parameter 1 to be mysqli PHP 2
C ERRORE: Warning: move_uploaded_file()... come risolverlo? PHP 1
N warning su sessione PHP 2
A Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given PHP 17
L Problema messaggio d'errore -Warning: mysqli_num_rows() expects parameter 1 to be mysqli_ etc etc... PHP 2
pup3770 Su Mac funziona - su windows da warning PHP 9
E Warning: session_start() PHP 1
P Warning: mysql_num_rows() expects parameter 1 to be resource, array given in PHP 9
A Warning: mysql_result() expects parameter 1 to be resource, boolean given MySQL 2
I Warning: Division by zero in Joomla 0
S Help : Warning: mail() [function.mail] PHP 2
H Warning: Cannot modify header information... PHP 6
P Warning email() PHP 116
catellostefano CLASSE PHP Warning: mysql_fetch_assoc() expects parameter 1 to be resource PHP 2
Frank10 Problema w3c warning HTML5 -.-" HTML e CSS 5
neo996sps Warning su redirect della pagina PHP 9
M Warning: mysql_query() expects parameter 2 to be resource, boolean given PHP 3
S Warning sulle sessioni, come fare? PHP 1
catellostefano Problema per richiamare file xml -- Warning: DOMDocument::load() [domdocument.load]: PHP 0
L Warning: mysql_fetch_assoc() expects parameter 1 to be resource, null given in .... PHP 2
L Warning: Cannot modify header information - headers already sent by... PHP 9
H Warning: session_start() [function.session-start]: Cannot send session cache limiter PHP 8
S errore warning mysql_ferch_array() e mysql_free_result() PHP 5
A Warning strano PHP 2
JellyBelly Warning Uso Rename PHP 2
M Warning: session_start(): Cannot send session cache... ???? PHP 5
W Warning: main(./WEB-INF/etc/config.php): PHP 3
M Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in PHP 6
M Warning: Cannot modify header information - headers already sent by (output started a PHP 1
C problema con un warning PHP 0
otto9due SQLSTATE[HY093]: Invalid parameter number: parameter was not defined ? Perchè secondo voi? PHP 5
B [JAVA] java.io.StreamCorruptedException: invalid type code: AC Java 1
T [PHP] SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of PHP 1
M W3C Invalid Output HTML e CSS 9
Silen invalid argument foreach PHP 4
Monital mysql_num_rows(): supplied argument is not a valid MySQL PHP 2
S Errori supplied argument ?!!??!?! PHP 1

Discussioni simili