Your SlideShare is downloading. ×
Prog web 07-pdo
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Prog web 07-pdo

236
views

Published on

Published in: Technology, Education

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
236
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Programação para WEBRegis Pires Magalhãesregispiresmag@gmail.comPDOPHP Data Objects
  • 2. PDO Interface comum para acesso a vários bancos de dados; Escrito em C e com excelente performance; Suporte a: MySQL, PostgreSQL, Firebird, SQLite,Oracle, ODBC, MSSQL, Sybase, Informix, FreeTDS; Suporta Prepared Statements, Parâmetros Nomeadose Conexões Persistentes; Usa UNIX Socket caso o host seja definido comolocalhost e tal recurso esteja disponível:$con = new PDO(mysql:host=localhost;dbname=prog_web,root, ); Pode-se também definir o uso de UNIX Socketexplicitamente:$con = new PDO(mysql:unix_socket;dbname=prog_web,root, );
  • 3. MySQL via PDO<?phptry {$con = new PDO(mysql:host=localhost;dbname=prog_web,root, );$stmt = $con->prepare("SELECT * FROM pessoas ");$stmt->execute();$resultado = $stmt->fetchAll();print_r($resultado);} catch (PDOException $ex) {echo $ex->getMessage();die();}?>
  • 4. MySQL via PDO<?phptry {$nome = R;$con = new PDO(mysql:host=localhost;dbname=prog_web,root, , array(PDO::ATTR_PERSISTENT => true,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));$sql = "SELECT * FROM pessoasWHERE nome LIKE concat(:nome,%)";$stmt = $con->prepare($sql);$stmt->bindParam(:nome, $nome);$stmt->setFetchMode(PDO::FETCH_ASSOC);$stmt->execute();$resultado = $stmt->fetchAll();print_r($resultado);} catch (PDOException $ex) {echo $ex->getMessage();die();}?>
  • 5. MySQL via PDO<?phptry {$nome = Regis;$fone = 1234;$email = regis@cefetpi.br;$con = new PDO(mysql:host=localhost;dbname=prog_web,root, , array(PDO::ATTR_PERSISTENT => true,PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));$sql = INSERT INTO pessoas (nome,fone,email)VALUES (:nome,:fone,:email) ;$stmt = $con->prepare($sql);$stmt->bindParam(:nome, $nome);$stmt->bindParam(:fone, $fone);$stmt->bindParam(:email, $email);$stmt->execute();} catch (PDOException $ex) {echo $ex->getMessage();die();}?>
  • 6. Obtendo informações sobre PDO<?php$con = new PDO(mysql:host=localhost;dbname=prog_web,root, );$attributes = array("AUTOCOMMIT", "ERRMODE", "CASE", "CLIENT_VERSION","CONNECTION_STATUS", "ORACLE_NULLS", "PERSISTENT","PREFETCH", "SERVER_INFO", "SERVER_VERSION","TIMEOUT");foreach ($attributes as $val) {echo "PDO::ATTR_$val: ";echo $con->getAttribute(constant("PDO::ATTR_$val")) .<br/>;}?>
  • 7. Obtendo informações sobre PDO<?php$con = new PDO(mysql:host=localhost;dbname=prog_web,root, );$attributes = array("AUTOCOMMIT", "ERRMODE", "CASE", "CLIENT_VERSION","CONNECTION_STATUS", "ORACLE_NULLS", "PERSISTENT","PREFETCH", "SERVER_INFO", "SERVER_VERSION","TIMEOUT");foreach ($attributes as $val) {echo "PDO::ATTR_$val: ";echo $con->getAttribute(constant("PDO::ATTR_$val")) .<br/>;}?>