Last active
August 29, 2015 14:24
-
-
Save jhonata-menezes-forseti/9974c097329ce121e439 to your computer and use it in GitHub Desktop.
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
/* | |
* Referencia: http://www.diogomatheus.com.br/blog/php/trabalhando-com-pdo-no-php/ | |
*/ | |
$pdo = new PDO("mysql:host=localhost;dbname=test", 'root', '', [ | |
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8' | |
]); | |
/* | |
* tambem ha essa opção de configurar a conexão | |
*/ | |
#$pdo->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, 'SET NAMES utf8'); | |
/* | |
* Exemplo de um insert utilizando PDO | |
*/ | |
$stmt = $pdo->prepare('insert into aluno (nome, ra) VALUES (:nome, :ra)');//preparando a SQL | |
$stmt->bindValue(':nome', $nome);// relacionando o nome ao valor | |
$stmt->bindValue(':ra', $ra); | |
$stmt->execute();// apos o execute ele insere no banco | |
/* | |
* Utilizando select com o PDO | |
*/ | |
$stmt = $pdo->prepare("SELECT * FROM aluno"); | |
while($row = $stmt->fetch()) {// A cada iteração, ele traz a proxima linha do registro | |
printf("Nome: %s RA: %s \n", $row->nome, $row->ra); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment