public function index() { $db = Factory::getDatabase(); $res = $db->getRow('select * from user'); print_r($res); echo '<br>'; $db->exec('UPDATE user SET password ="******" LIMIT 1'); }
public function __call($method, $args) { $sql = $args[0]; if (strtolower(substr($sql, 0, 6)) == 'select') { echo '读操作<br>'; $db = Factory::getDatabase('slave'); return call_user_func_array([$db, $method], $args); } else { echo '写操作<br>'; $db = Factory::getDatabase('master'); return call_user_func_array([$db, $method], $args); } }
<?php /** * 读取Reis里的用户注册信息队列,写入到数据库 * todo: 将此脚本开启自动启动 * 问题: 写入数据库失败怎么办? 这个进行停掉了怎么办? */ require '../../int.inc.php'; $flag = true; use Tiny\MessageQueue\UserMQ; $mq = UserMQ::MQ(); while ($flag) { $data = $mq->pop(); if ($data) { $db = \Tiny\Service\Factory::getDatabase(); $sql = "INSERT INTO user\n SET username = ?,\n email = ?"; $db->exec($sql, $data['username'], $data['email']); } }
/** * 测试PDO */ public function pdo() { $db = \Tiny\Service\Factory::getDatabase(); $res = $db->getRow('select * from user'); print_r($res); }
public function __construct() { $this->db = Factory::getDatabase(); }