Mysql tabella output in JSON

Jakub Lemiszewski

Utente Attivo
5 Dic 2013
119
1
0
Salve,
sto cercando di prendere una tabella MYSql e immetterla in jason in output a schermo.
Il codice seguente non mi fa nulla e non mi da errori e non capisco quale sia il problema:
PHP:
<?php
    //Create Database connection

$servername = "localhost";
$username   = "root";
$password   = "";
$dbname     = "demo";

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);

// Check connection
$conn or die(mysql_error());
    //Query.

$sql    = "SELECT * FROM countries";
$result = mysqli_query($conn, $sql) or die("Error in Selecting " . mysqli_error($conn));

    //Create an array

$json_response[] = array();

while ($row =mysqli_fetch_assoc($result)) {

    $row_array['code'] = $row['code'];

    $row_array['name_en'] = $row['name_en'];

    $row_array['name_fr'] = $row['name_fr'];

    //push the values in the array

    array_push($json_response,$row_array);

}
$jsonOtput = json_encode($json_response, JSON_PRETTY_PRINT);
echo $jsonOtput;

    //Close the database connection

$conn->close();

?>
Grazie per l'aiuto.
 

marino51

Utente Attivo
28 Feb 2013
3.204
207
63
Lombardia
ho usato la stessa json array, a me funziona,
vedi di aver usato le "ASSOC" giuste
ciao
Marino

PHP:
$sql = "SELECT * FROM PERIODI WHERE id_periodo IN( ?, ?, ? ) and id_struttura=?";

print( "<br />-------------------- json array <br />" );
  $sth = $db->query( $sql, array( 11, 13, 15, 2 ) );

  $json_response = array(); 

  while( $row = $sth->fetch( PDO::FETCH_ASSOC ) ) {
    $json_response['id_periodo']	= $row['id_periodo']; 
    $json_response['id_struttura']	= $row['id_struttura']; 
    $json_response['data_inizio']	= $row['data_inizio']; 
    $json_response['data_fine']		= $row['data_fine']; 
  }
  $jsonOtput = json_encode($json_response, JSON_PRETTY_PRINT); 
  echo $jsonOtput."<br /><br />";

Codice:
-------------------- json array 
SELECT * FROM PERIODI WHERE id_periodo IN( 11, 13, 15 ) and id_struttura=2 

{ "id_periodo": "13", "id_struttura": "2", "data_inizio": "2014-04-15 00:00:00.000", "data_fine": "2014-04-30 00:00:00.000" }
 
Ultima modifica:

Jakub Lemiszewski

Utente Attivo
5 Dic 2013
119
1
0
Grazie sono riuscito a farlo funzionare ma mi istituisce solo una riga e vorrei vedere tutto.
Mi sapresti dire come cambiare il codice per poter vedere tutto in json.
Grazie mille.
 
Discussioni simili
Autore Titolo Forum Risposte Data
S problema con recupero dati tabella mysql PHP 2
E Progressbar estrazione dati da tabella mySQL Ajax 9
D controllare valore in tabella mysql PHP 0
B Crea pdf da tabella mysql "ultima riga modificata" MySQL 4
elpirata Query per leggere dati da una tabella mysql e mostrarli a video in base a parametri passati tramite GET PHP 5
R Errore UPDATE tabella mysql PHP 1
G Rendere dati di una tabella mysql link PHP 22
B Fare calcoli con php da tabella mysql PHP 2
R Visualizzazione tabella Mysql MySQL 0
R [MySQL] importare tabella cvs MySQL 0
R anomalia nella visualizzazione tabella mysql MySQL 1
B inserire valori da una tabella a un altra mysql PHP 34
E Inserimento dati da PHP in tabella MySQL PHP 5
M [MySQL] CREARE UN LINK SU UN DETERMINATO CAMPO DI UNA TABELLA RISULTATO DI UNA QUERY SQL MySQL 3
elpirata [MYSQL] Schedulare evento per update del campo data su tabella MySQL 0
V popolare tabella da mysql PHP 7
G inserimento csv in tabella mysql; problema con struttura PHP 11
G [MySQL] Tabella piena MySQL 9
S [MySQL] Controllo dati di una tabella in tempo reale MySQL 0
V Creare tabella mysql con php non funziona PHP 1
bianca_dimulescu Gestione tabella e MySQL PHP 4
S gestire una tabella mysql PHP 9
G [MySQL] Errore importazione tabella .sql #1062 - Duplicate entry '592' for key 'PRIMARY' MySQL 20
Trabba [MySQL] Trasformare tabella con totali MySQL 2
S [MySQL] Sapere se una tabella è stata modificata... MySQL 0
M Aggioranre record in tabella PHP MYSQL PHP 2
G [MySQL] Conteggio records su tabella relazionata MySQL 3
Primian Importare dati su tabella MySQL MySQL 3
myprobsql Alias per i valori in un campo di una tabella Database MySql PHP 1
M Inserimento Array prelevato da Database in Php in un altra tabella mysql PHP 0
S recuperare un dato da una tabella mysql per inserirlo in una casella di input jQuery 3
F Spostare dati tabella Mysql in un campo solo di un'altra tabella PHP 1
E php Mysql visualizzazione file *.php in tabella PHP 0
giancadeejay Filtrare dati tabella mysql ed estrarli PHP 4
A esportare dati da tabella mysql a excel mantenendo formattazione PHP 0
C [MySQL] errore nell'importazione tabella con file csv MySQL 0
B [MySQL] copiare campo da tabella ad altra ed eliminarlo MySQL 0
B [MySQL] INSERIRE UTENTE CONNESSO IN TABELLA MySQL 2
K Mostrare tabella PHP e MySQL PHP 0
S Php e mysql, estrazione da una tabella e inserimento in un'altra tabella PHP 14
H problema input e echo più linee di una tabella mysql PHP 13
Z tabella editabile da un DB mysql remoto PHP 1
E link da tabella Mysql a pagina di dettaglio PHP 2
L Modificare dati tabella mysql PHP 2
P Aggiornare Tabella con form ma senza Mysql HTML e CSS 0
A suddivisione dati mysql in tabella PHP 15
neo996sps [PHP + MySQL] Tabella 5 colonne per N righe con un record per cella PHP 3
A form che si refrescia dopo modifica tabella mysql PHP 2
francesco7 Aiuto! Creazione di una tabella in MySql PHP 2
G Aggiornare colonna tabella automaticamente in MysQl PHP 2

Discussioni simili