Created
September 8, 2010 18:00
-
-
Save dgmike/570516 to your computer and use it in GitHub Desktop.
Simple way to use PDO
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 | |
| class Con extends PDO | |
| { | |
| function Con() | |
| { | |
| self::PDO(CONFIG_DNS, CONFIG_USER, CONFIG_PASS); | |
| } | |
| function get($table, $id) | |
| { | |
| return $this->query("SELECT * FROM $table WHERE id = $id"); | |
| } | |
| function all($table) | |
| { | |
| return $this->query("SELECT * FROM $table"); | |
| } | |
| } | |
| // Using... | |
| $con = new Con; | |
| $book = $con->get('book', 3); | |
| echo $book->title; | |
| // using all books | |
| foreach ($con->all('book') as $book) { | |
| echo $book->author; | |
| } |
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 | |
| // Simple way to use PDO | |
| function getFruit($conn) { | |
| $sql = 'SELECT name, color, calories FROM fruit ORDER BY name'; | |
| foreach ($conn->query($sql) as $row) { | |
| print $row['name'] . "\t"; | |
| print $row['color'] . "\t"; | |
| print $row['calories'] . "\n"; | |
| } | |
| } | |
| // Using... | |
| $pdo = new PDO($dsn,$user,$pass); | |
| getFruit($pdo); |
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 | |
| $login = 'login'; | |
| $senha = md5('senha'); | |
| $con = new Con; | |
| $sth = $con->prepare('SELECT nome FROM usuario WHERE login = ? AND senha = ?'); | |
| $sth->execute(array($login, $senha)); | |
| $result = $sth->fetch(PDO::FETCH_OBJ); | |
| print $result->nome; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment