Skip to content

Instantly share code, notes, and snippets.

@cesc1989
Last active April 5, 2017 03:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cesc1989/b8b5f19702db16a98eb6 to your computer and use it in GitHub Desktop.
Save cesc1989/b8b5f19702db16a98eb6 to your computer and use it in GitHub Desktop.
conect to a database and query it to return data in json format
<?php
$numero_cedula = $_GET['cedula'];
//CONEXION A LA BASE DE DATOS
$username = "root";
$password = "root";
$hostname = "localhost";
$link = mysql_connect($hostname, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//SELECCIONAR BASE DE DATOS
$db = mysql_select_db("qhp", $link);
if (!$db) {
die ("No se puede usar la base de datos" . mysql_error());
}
// HACER CONSULTA A LA TABLA DE AFILIADOS
$query = "SELECT first_name, last_name, uid, validity, typeofmembership FROM users WHERE numdoc='$numero_cedula'";
$result = mysql_query($query);
if (!$result){
die("Consulta invalida " . mysql_error());
}
$array = mysql_fetch_row($result);
echo json_encode($array);
// else{
// while ($row = mysql_fetch_assoc($result)) {
// echo $row['first_name'];
// echo $row['last_name'];
// echo $row['uid'];
// echo $row['validity'];
// }
// }
// CERRAR LA CONEXION LUEGO DE HACER TODAS LAS OPERACIONES
mysql_close($link);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment