Last active
August 30, 2016 21:53
-
-
Save joffilyfe/dff89cd85c59a6140543d6a5db314780 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 | |
header('Content-Type: application/json'); | |
/* | |
create table pessoa ( cpf varchar(255), nome varchar(255), sexo varchar(255), datanascimento varchar(255), rua varchar(255), numero varchar(255), bairro varchar(255), cidade varchar(255), estado varchar(255) ); | |
INSERT INTO pessoa (cpf, nome, sexo, datanascimento, rua, numero, bairro, cidade, estado) | |
VALUES ('1', 'joffily', 'm', '09/10/1989', 'JSF', '99', 'Novo', 'Guarabira', 'Paraíba'); | |
INSERT INTO pessoa (cpf, nome, sexo, datanascimento, rua, numero, bairro, cidade, estado) | |
VALUES ('2', 'Ryan', 'm', '09/10/1989', 'JSF', '99', 'Novo', 'Guarabira', 'Paraíba'); | |
CREATE TABLE telefones ( | |
cpf varchar(255), | |
telefone varchar(255) | |
); | |
INSERT INTO telefones (cpf, telefone) VALUES ('1', '99994780'); | |
INSERT INTO telefones (cpf, telefone) VALUES ('1', '99994781'); | |
INSERT INTO telefones (cpf, telefone) VALUES ('1', '99994782'); | |
*/ | |
?> | |
<?php | |
function search_pessoa($db, $cpf) { | |
$sql = "SELECT * FROM pessoa WHERE cpf LIKE :cpf"; | |
$smt = $db->prepare($sql); | |
$smt->bindValue(':cpf', $cpf); | |
$smt->execute(); | |
$pessoas = $smt->fetchAll(PDO::FETCH_ASSOC); //FETCH_ASSOC | |
$telefonesArray = array(); | |
foreach($pessoas as &$pessoa) { | |
$sql = "SELECT * FROM telefones WHERE cpf LIKE :cpf"; | |
$smt = $db->prepare($sql); | |
$smt->bindParam(':cpf', $pessoa['cpf']); | |
$smt->execute(); | |
$telefones = $smt->fetchAll(PDO::FETCH_ASSOC); | |
foreach($telefones as $telefone) { | |
$telefonesArray[] = $telefone['telefone']; | |
} | |
$pessoa['telefones'] = $telefonesArray; | |
} | |
return $pessoas; | |
} | |
try { | |
$db = new PDO('mysql:host=localhost;dbname=pweb', 'root', ''); | |
if ($_GET && isset($_GET['cpf'])) { | |
$pessoas = search_pessoa($db, $_GET['cpf']); | |
foreach ($pessoas as $key => $value) { | |
echo json_encode($value); | |
} | |
} | |
} catch (Exception $e) { | |
var_dump($e->getMessage()); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment