Last active
December 19, 2015 08:09
-
-
Save hugollm/5923752 to your computer and use it in GitHub Desktop.
Exemplo de conexão/consulta a banco MySql usando PDO.
This file contains 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 | |
// parametros de conexão | |
$host = 'localhost'; | |
$dbname = 'arsenal'; | |
$username = 'root'; | |
$password = ''; | |
// opcional, porém importante | |
$options = array( | |
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, | |
PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'", | |
); | |
// conectando | |
$pdo = new PDO('mysql:host=localhost;dbname='.$dbname, $username, $password, $options); | |
// executando query | |
$stm = $pdo->prepare('SELECT * FROM sessions'); | |
$stm->execute(); | |
$objectArray = $stm->fetchAll(PDO::FETCH_OBJ); | |
// imprimindo json | |
$json = json_encode($objectArray); | |
echo $json; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment