Browse Source

Use the prepared statement directly (#5086)

* Use the prepared statement directly

* Change PDO for postgre
Joan Miquel 5 years ago
parent
commit
8e06b89566

+ 4 - 5
frameworks/PHP/workerman/dbraw.php

@@ -1,14 +1,13 @@
 <?php
 <?php
 function dbraw()
 function dbraw()
 {
 {
-    global $pdo;
-    static $statement;
+    global $statement;
 
 
-    $statement = $statement ?? $pdo->prepare('SELECT id,randomNumber FROM World WHERE id = ?');
+    //$statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
 
 
     if ( ! isset($_GET['queries'])) {
     if ( ! isset($_GET['queries'])) {
         $statement->execute([mt_rand(1, 10000)]);
         $statement->execute([mt_rand(1, 10000)]);
-        return json_encode($statement->fetch(PDO::FETCH_ASSOC));
+        return json_encode($statement->fetch());
     }
     }
 
 
     $query_count = 1;
     $query_count = 1;
@@ -18,7 +17,7 @@ function dbraw()
 
 
     while ($query_count--) {
     while ($query_count--) {
         $statement->execute([mt_rand(1, 10000)]);
         $statement->execute([mt_rand(1, 10000)]);
-        $arr[] = $statement->fetch(PDO::FETCH_ASSOC);
+        $arr[] = $statement->fetch();
     }
     }
 
 
     return json_encode($arr);
     return json_encode($arr);

+ 8 - 7
frameworks/PHP/workerman/fortune.php

@@ -1,13 +1,13 @@
 <?php
 <?php
 function fortune()
 function fortune()
 {
 {
-    global $pdo;
-    static $statement;
-    $statement = $statement ?? $pdo->prepare('SELECT id,message FROM Fortune');
-    $statement->execute();
+    global $fortune;
 
 
-    $arr       = $statement->fetchAll(PDO::FETCH_KEY_PAIR);
-    $arr[0]    = 'Additional fortune added at request time.';
+    //$fortune = $pdo->prepare('SELECT id,message FROM Fortune');
+    $fortune->execute();
+
+    $arr    = $fortune->fetchAll(PDO::FETCH_KEY_PAIR);
+    $arr[0] = 'Additional fortune added at request time.';
     asort($arr);
     asort($arr);
 
 
     $html = '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>';
     $html = '<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>';
@@ -15,5 +15,6 @@ function fortune()
         $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
         $message = htmlspecialchars($message, ENT_QUOTES, 'UTF-8');
         $html .= "<tr><td>{$id}</td><td>{$message}</td></tr>";
         $html .= "<tr><td>{$id}</td><td>{$message}</td></tr>";
     }
     }
-    return $html . '</table></body></html>';
+
+    return $html.'</table></body></html>';
 }
 }

+ 9 - 4
frameworks/PHP/workerman/server.php

@@ -7,12 +7,16 @@ use Workerman\Protocols\Http;
 use Workerman\Worker;
 use Workerman\Worker;
 
 
 $http_worker                = new Worker('http://0.0.0.0:8080');
 $http_worker                = new Worker('http://0.0.0.0:8080');
-$http_worker->count         = (int) shell_exec('nproc') ?? 64;
+$http_worker->count         = shell_exec('nproc');
 $http_worker->onWorkerStart = function () {
 $http_worker->onWorkerStart = function () {
-    global $pdo;
+    global $pdo, $fortune, $statement;
     $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world',
     $pdo = new PDO('mysql:host=tfb-database;dbname=hello_world',
-        'benchmarkdbuser', 'benchmarkdbpass');
+        'benchmarkdbuser', 'benchmarkdbpass',
+        [PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC]);
+    $fortune   = $pdo->prepare('SELECT id,message FROM Fortune');
+    $statement = $pdo->prepare('SELECT id,randomNumber FROM World WHERE id=?');
 };
 };
+
 $http_worker->onMessage = static function ($connection) {
 $http_worker->onMessage = static function ($connection) {
 
 
     Http::header('Date: '.gmdate('D, d M Y H:i:s').' GMT');
     Http::header('Date: '.gmdate('D, d M Y H:i:s').' GMT');
@@ -45,7 +49,8 @@ $http_worker->onMessage = static function ($connection) {
             //   $connection->send(ob_get_clean());
             //   $connection->send(ob_get_clean());
 
 
             //default:
             //default:
-            //   $connection->send('error');
+            //   Http::header('HTTP', true, 404);
+            //   $connection->send('Error 404');
     }
     }
 };
 };
 
 

+ 21 - 20
frameworks/PHP/workerman/updateraw.php

@@ -1,25 +1,26 @@
 <?php
 <?php
-function updateraw() {
-  global $pdo;
-  $query_count = 1;
-  if ($_GET['queries'] > 1) {
-    $query_count = min($_GET['queries'], 500);
-  }
+function updateraw()
+{
+    global $pdo;
+    $query_count = 1;
+    if ($_GET['queries'] > 1) {
+        $query_count = min($_GET['queries'], 500);
+    }
 
 
-  $statement = $pdo->prepare('SELECT randomNumber FROM World WHERE id = ?');
-  $updateStatement = $pdo->prepare('UPDATE World SET randomNumber = ? WHERE id = ?');
+    $statement       = $pdo->prepare('SELECT randomNumber FROM World WHERE id=?');
+    $updateStatement = $pdo->prepare('UPDATE World SET randomNumber=? WHERE id=?');
 
 
-  while ($query_count--) {
-    $id = mt_rand(1, 10000);
-    $statement->execute([$id]);
-    
-    $world = ['id' => $id, 'randomNumber' => $statement->fetchColumn()];
-    $updateStatement->execute(
-      [$world['randomNumber'] = mt_rand(1, 10000), $id]
-    );
- 
-    $arr[] = $world;
-  }
+    while ($query_count--) {
+        $id = mt_rand(1, 10000);
+        $statement->execute([$id]);
 
 
-  return json_encode($arr);
+        $world = ['id' => $id, 'randomNumber' => $statement->fetchColumn()];
+        $updateStatement->execute(
+            [$world['randomNumber'] = mt_rand(1, 10000), $id]
+        );
+
+        $arr[] = $world;
+    }
+
+    return json_encode($arr);
 }
 }