12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- <?php
- class RawController extends AppController
- {
- protected $pdo;
- protected function before_filter()
- {
- View::select(null, null);
- header('Content-type: application/json');
- $this->pdo = new PDO('mysql:host=tfb-database;dbname=hello_world', 'benchmarkdbuser', 'benchmarkdbpass', array(
- PDO::ATTR_PERSISTENT => true
- ));
- }
- public function index()
- {
- $statement = $this->pdo->query( 'SELECT id,randomNumber FROM World WHERE id = '. mt_rand(1, 10000) );
- echo json_encode($statement->fetch(PDO::FETCH_ASSOC));
- }
- public function queries($count = 1)
- {
- $count = min(max($count, 1), 500);
- $res = $this->pdo->prepare('SELECT id,randomNumber FROM World WHERE id = ?');
- $worlds = [];
- for ($i = 0; $i < $count; ++$i) {
- $res->execute([mt_rand(1, 10000)]);
- $worlds[] = $res->fetch(PDO::FETCH_ASSOC);
- }
- echo json_encode($worlds);
- }
- public function update($count = 1)
- {
- $count = min(max($count, 1), 500);
- $worlds = [];
-
- $sth = $this->pdo->prepare('SELECT * FROM World WHERE id = ?');
- $updateSth = $this->pdo->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
-
- for ($i = 0; $i < $count; ++$i) {
- $id = mt_rand(1, 10000);
- $randomNumber = mt_rand(1, 10000);
- $sth->execute(array($id));
- $row = ['id' => $id, 'randomNumber' => $updateSth->fetchColumn()];
- $row['randomNumber'] = $randomNumber;
- $updateSth->execute([$randomNumber, $id]);
- $worlds[] = $row;
- }
- echo json_encode($worlds);
- }
- }
|