Last active
May 22, 2016 16:50
-
-
Save Ferrmolina/22abecf8df02d67917d5ff1ebb8a6489 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('HOSTNAME', 'hostname_here'); | |
define('USERNAME', 'username_here'); | |
define('PASSWORD', 'password_here'); | |
define('DATABASE', 'name_here'); | |
$enlace = new mysqli(HOSTNAME, USERNAME, PASSWORD, DATABASE); | |
$enlace->set_charset('utf8'); | |
$ejecutar = "SELECT * FROM tabla"; | |
$resultado = $enlace->query($ejecutar) or trigger_error($enlace->error." [sql error]"); | |
$contar = $resultado->num_rows; | |
$response = ""; | |
if ($contar == 0) { | |
$response['cantidad'] = 0; | |
echo json_encode($response); | |
} else { | |
$n = 1; | |
while ($row = $resultado->fetch_assoc()) { | |
$response['cantidad'] = $contar; | |
$response['fila-'.$n] = $row; | |
$n++; | |
} | |
echo json_encode($response, JSON_UNESCAPED_UNICODE); | |
} | |
// Output: | |
// { | |
// "cantidad": 1, | |
// "fila-1": { | |
// "nombre_columna_1":"valor_registro_columna_1", | |
// "nombre_columna_2":"valor_registro_columna_2", | |
// "nombre_columna_3":"valor_registro_columna_3", | |
// }, | |
// "fila-2": { | |
// "nombre_columna_1":"valor_registro_columna_1", | |
// "nombre_columna_2":"valor_registro_columna_2", | |
// "nombre_columna_3":"valor_registro_columna_3", | |
// }, | |
// ... | |
// } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment