[PHP] [SOLVED] While query result

Mirco#0099FF

Nuovo Utente
22 Gen 2019
6
0
1
Salve a tutti, scusate la domanda forse un po' banale, nel mio progetto è incluso questa classe che uso per lanciare e preparare le query [https://github.com/tschoffelen/db.php/blob/master/Database.php] questo è il file (il mio ovviamente è leggermente diverso ma non varia il problema)
Dovrei generare un ciclo while simile a questo
PHP:
while($row = mysqli_fetch_assoc($query))
{
   // code ...
}
ma non riesco a capire come fare con il mysqli_fetch_assoc, qualcuno può aiutarmi?

Scusate ma è la prima volta che lavoro con le classi su PHP :(
 
PHP:
while($row = mysqli_fetch_assoc($query))
{
   foreach($row as $chiave=>$valore){
       echo $chiave.' - '. $valore.'<br>';
   }
}
 
PHP:
while($row = mysqli_fetch_assoc($query))
{
   foreach($row as $chiave=>$valore){
       echo $chiave.' - '. $valore.'<br>';
   }
}

Mi sa che non hai capito il problema di fondo, ti allego io mio codice con l'errore riportato
PHP:
$query = $db->select($GLOBALS['tblUsers'], FALSE, 1, FALSE, FALSE, '*')->row();
while ($row = mysqli_fetch_assoc($query))
{
   // code ...
}

e l'errore in questione è questo:
"Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, object given in /Applications/XAMPP/xamppfiles/htdocs/dashboard/index.php on line 7"
 
Immagino che la documentazione tu non l'abbia letta
https://github.com/tschoffelen/db.php
Reading results
Reading the results can be done with the following functions:

  • $db->count() returns the number of selected rows, equal to mysql_num_rows()

  • $db->result() returns all matches rows as an array containing row objects

  • $db->row() returns the first row that matches the query as an object

  • $db->result_array() returns all matches rows as an array containing row arrays

  • $db->row_array() returns the first row that matches the query as an array
Quindi :
PHP:
$result=$db->select($GLOBALS['tblUsers'], FALSE, 1, FALSE, FALSE, '*')->row_array();
foreach($result as $chiave=>$valore){
    echo $chiave.' - '. $valore.'<br>';
}
 
Immagino che la documentazione tu non l'abbia letta
https://github.com/tschoffelen/db.php
Reading results
Reading the results can be done with the following functions:

  • $db->count() returns the number of selected rows, equal to mysql_num_rows()

  • $db->result() returns all matches rows as an array containing row objects

  • $db->row() returns the first row that matches the query as an object

  • $db->result_array() returns all matches rows as an array containing row arrays

  • $db->row_array() returns the first row that matches the query as an array
Quindi :
PHP:
$result=$db->select($GLOBALS['tblUsers'], FALSE, 1, FALSE, FALSE, '*')->row_array();
foreach($result as $chiave=>$valore){
    echo $chiave.' - '. $valore.'<br>';
}
L'avevo letta ma non avevo pensato a questa soluzione, grazie mille.
 
Immagino che la documentazione tu non l'abbia letta
https://github.com/tschoffelen/db.php
Reading results
Reading the results can be done with the following functions:

  • $db->count() returns the number of selected rows, equal to mysql_num_rows()

  • $db->result() returns all matches rows as an array containing row objects

  • $db->row() returns the first row that matches the query as an object

  • $db->result_array() returns all matches rows as an array containing row arrays

  • $db->row_array() returns the first row that matches the query as an array
Quindi :
PHP:
$result=$db->select($GLOBALS['tblUsers'], FALSE, 1, FALSE, FALSE, '*')->row_array();
foreach($result as $chiave=>$valore){
    echo $chiave.' - '. $valore.'<br>';
}
Però ti faccio una domanda, per stampare un solo valore (come ad esempio $row['id']) come posso fare? Scusa la domanda banale
 
PHP:
$result=$db->select($GLOBALS['tblUsers'], FALSE, 1, FALSE, FALSE, '*')->row_array();
foreach($result as $chiave=>$valore){
    echo $valore['id'];
}
in alternativa
PHP:
echo $result[0]['id'];
in alternativa
PHP:
for($i=0;$i<count($result);$i++){
echo $result[$i]['id'];
}
 
PHP:
$result=$db->select($GLOBALS['tblUsers'], FALSE, 1, FALSE, FALSE, '*')->row_array();
foreach($result as $chiave=>$valore){
    echo $valore['id'];
}
in alternativa
PHP:
echo $result[0]['id'];
in alternativa
PHP:
for($i=0;$i<count($result);$i++){
echo $result[$i]['id'];
}
No, le ho provate tutte ma non funzionano, dovrei creare una tabella con quei dati e non riesco a selezionarne un singolo campo :(

Scusa mi spiego meglio, di per se funziona il metodo che mi hai detto (anche se ho usato $result['id] perché altrimenti dava errore) ma nonostante ci sia un solo record nel database a me stampa 16 volte il risultato (16 di fatto sono i campi del database)
 
Ultima modifica:
Scusa mi spiego meglio, di per se funziona il metodo che mi hai detto (anche se ho usato $result['id] perché altrimenti dava errore) ma nonostante ci sia un solo record nel database a me stampa 16 volte il risultato (16 di fatto sono i campi del database)
Scusa, leggi la documentazione invece di andare a tentativi....
se il problema è la stampa fai un
PHP:
print_r($result)
e vedi di che natura è la variabile.... in questo modo dovresti riuscire a capire che tipo di dato devi trattare.
 
Scusa, leggi la documentazione invece di andare a tentativi....
se il problema è la stampa fai un
PHP:
print_r($result)
e vedi di che natura è la variabile.... in questo modo dovresti riuscire a capire che tipo di dato devi trattare.
L'oggetto in questione è un array ma non capisco perché mi stampa ogni risultato 16 volte, come se invece di dire (per ogni riga prendesse per ogni colonna)
 
Scusa, leggi la documentazione invece di andare a tentativi....
se il problema è la stampa fai un
PHP:
print_r($result)
e vedi di che natura è la variabile.... in questo modo dovresti riuscire a capire che tipo di dato devi trattare.
Io ho un problema diverso, non riesco a trovare il modo di eseguire una specifica query con questa classe, la query è questa, potete aiutarmi


Codice:
SELECT `id` FROM `nwm_post` WHERE NOT EXISTS (SELECT who_block FROM nwm_block WHERE `author` = who_is_blocked AND who_block = 1) AND NOT EXISTS (SELECT who_block FROM nwm_block WHERE `author` = who_block AND who_is_blocked = 1) AND `deleted` = FALSE ORDER BY `timestamp` DESC
 
Scusa mi spiego meglio, di per se funziona il metodo che mi hai detto (anche se ho usato $result['id] perché altrimenti dava errore) ma nonostante ci sia un solo record nel database a me stampa 16 volte il risultato (16 di fatto sono i campi del database)
Ripeto.... nell variabile result ci sta direttamente la riga completa prelevata dal DB, quindi stampa 16 volte perchè ci sono (forse) 16 colonne....
e vedi di che natura è la variabile....
Ossia è array di oggetti o è un oggetto?

Quindi nel caso di array di obj:
PHP:
$result[0]->id
altrimenti
PHP:
$result->id

Notazione obj o array sono identiche cambia solo la scrittura da
PHP:
$obj->obj
//a
$obj['obj]
  • $db->count() returns the number of selected rows, equal to mysql_num_rows()

  • $db->result() returns all matches rows as an array containing row objects

  • $db->row() returns the first row that matches the query as an object

  • $db->result_array() returns all matches rows as an array containing row arrays

  • $db->row_array() returns the first row that matches the query as an array
$ db-> count () restituisce il numero di righe selezionate, uguale a mysql_num_rows ()
$ db-> result () restituisce tutte le righe corrispondenti come una matrice contenente oggetti riga
$ db-> row () restituisce la prima riga che corrisponde alla query come oggetto
$ db-> result_array () restituisce tutte le righe corrispondenti come una matrice contenente matrici di righe
$ db-> row_array () restituisce la prima riga che corrisponde alla query come una matrice
Ti trovi nell'ultimo caso.... quindi direttamente array chiave valore
 
Ultima modifica:

Discussioni simili