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); } }
public function register() { if (!empty($_POST)) { var_dump($_POST); $username = $_POST['username']; $password = $_POST['password']; $redis = \Tiny\Service\Factory::getRedis(); //存储用户名和密码 $redis->set('string:user:'******'list:user', $username); $a = $redis->lRange('list:user', 0, -1); print_r($a); die; } }
<?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']); } }
/** * 测试Redis连接 */ public function redis() { $redis = \Tiny\Service\Factory::getRedis(); $redis->set('name', 'hanfeng'); echo $redis->get('name'); }
public function __construct() { $this->db = Factory::getDatabase(); }