PHP操作MYSQL--PDO
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                PHP操作MYSQL--PDO
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.                        
                                感覺比直接弄SQL語句高級,但還不到ORM的封裝。
一步一步進化。
app.json
{"db": {"user": "root","password": "xxxx","host": "10.2.3.4","port": "3306","dbname": "bookstore"} }config.php
<?phpnamespace Bookstore\Utils;use Bookstore\Exceptions\NotFoundException;require_once __DIR__ . '/NotFoundException.php';class Config {private $data;//類靜態(tài)變量,保證變量唯一性private static $instance;//構造函數(shù)私有化,類外部不可以調用.private function __construct() {$json = file_get_contents(__DIR__ . '/app.json');$this->data = json_decode($json, true);}//單例模式,保證只實例化一個類.public static function getInstance() {if (self::$instance == null) {//是可以自己實例化自己的.self::$instance = new Config();}return self::$instance;}public function get($key) {if (!isset($this->data[$key])) {throw new NotFoundException("Key $key not in config.");}return $this->data[$key];} } ?>test.php
<?php //使用命名空間,易于在大型應用中管理和組織php類.use Bookstore\Utils\Config;//命名空間可以直接use,但如果這個命名空間沒有在標準約定位置,且沒有自動載入的話,需要使用require來手工定位一下. require_once __DIR__ . '\Config.php';header("content-type:text/html;charset=utf-8"); $dbConfig = Config::getInstance()->get("db");$connStr = "mysql:host={$dbConfig['host']};port={$dbConfig['port']};dbname={$dbConfig['dbname']};charset=utf8";$db = new \PDO($connStr, $dbConfig['user'], $dbConfig['password']); $db->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_ASSOC);$query = 'SELECT * FROM book WHERE author = :author'; $statement = $db->prepare($query); $statement->bindValue('author', 'George Orwell'); $statement->execute(); $rows = $statement->fetchAll(); foreach ($rows as $row) {var_dump($row); } echo "<br/>"; $query = <<<SQL INSERT INTO book(isbn, title, author, price) VALUES(:isbn, :title, :author, :price) SQL; $statement = $db->prepare($query); $params = ['isbn' => '9781413108614','title' => 'Iliad','author' => 'Homer','price' => 9.25 ]; $statement->execute($params); $result = $db->exec($query); echo $db->lastInsertId(); echo "<br/>";function addBook(int $id, int $amount=1):void {$query = 'UPDATE book SET stock = stock + :n WHERE id = :id';$statement = $db->prepare($query);$statement->bindValue('id', $id);$statement->bindValue('n', $amount);if (!$statement->execute()) {throw new Exception($statement->errorInfo()[2]);} }function addSale($db, int $userId, array $bookIds):void {$db->beginTransaction();try {$query = 'INSERT INTO sale(customer_id, date)'. 'VALUES(:id, NOW())';$statement = $db->prepare($query);if (!$statement->execute(['id'=> $userId])) {throw new Exception($statement->errorInfo()[2]);}$saleId = $db->lastInsertId();$query = 'INSERT INTO sale_book(book_id, sale_id)'. 'VALUES(:book, :sale)';$statement = $db->prepare($query);$statement->bindValue('sale', $saleId);foreach ($bookIds as $bookId) {$statement->bindValue('book', $bookId);if (!$statement->execute()) {throw new Exception($statement->errorInfo()[2]);}}$db->commit();} catch (Exception $e) {$db->rollback();throw $e;} }try {addSale($db, 1, [1, 2, 300]); } catch (Exception $e) {echo 'Error adding sale: ' . $e->getMessage(); }try {addSale($db, 1, [1, 2, 3]); } catch (Exception $e) {echo 'Error adding sale: ' . $e->getMessage(); }?>輸出
array(6) { ["id"]=> string(1) "1" ["isbn"]=> string(13) "9780882339726" ["title"]=> string(4) "1984" ["author"]=> string(13) "George Orwell" ["stock"]=> string(2) "12" ["price"]=> string(3) "8.7" } array(6) { ["id"]=> string(1) "3" ["isbn"]=> string(13) "9780736692427" ["title"]=> string(11) "Animal Farm" ["author"]=> string(13) "George Orwell" ["stock"]=> string(1) "8" ["price"]=> string(4) "4.06" } 0 Error adding sale: Cannot add or update a child row: a foreign key constraint fails (`bookstore`.`sale_book`, CONSTRAINT `sale_book_ibfk_2` FOREIGN KEY (`book_id`) REFERENCES `book` (`id`))?
轉載于:https://www.cnblogs.com/aguncn/p/11133713.html
總結
以上是生活随笔為你收集整理的PHP操作MYSQL--PDO的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: MongonDB 知识
- 下一篇: 7.4 二分答案
