execute() public static method

Method to execute a statement in the database
public static execute ( string $json, string $use = null ) : array
$json string SQL statement in JSON format
$use string OPTIONAL Name of the database defined as a new connection instance
return array Returns an array with the number of rows affected by type of operation
Exemplo n.º 1
0
 public function testMultipleDeleteInJsonFormat()
 {
     // SQL Delete in JSON format
     $json = '
     delete: [
         { table: "books" , where: { id: 200 } } ,
         { table: "users" , where: { id: 1 } } ,
         { table: "users" , where: { id: 300 } } 
     ]
     ';
     // Executes the SQL and stores the result
     $result = test::execute($json);
     $this->assertEquals(1, self::getNumRowsAffected($result), 'Test with Delete command');
 }
Exemplo n.º 2
0
 static function delete($arr, $multi = '')
 {
     if ($arr) {
         if ($multi) {
             foreach ($arr as $i => $a) {
                 $delete[] = array("table" => self::$table, "where" => array("id" => $i));
             }
         } else {
             $delete[] = array("table" => self::$table, "where" => array("id" => $arr));
         }
         $json_delete = json_encode($delete);
         return parent::execute('delete:' . $json_delete);
     }
 }
Exemplo n.º 3
0
 /**
  * Main method
  * 
  * */
 public function __construct()
 {
     // Validates the form input submitted, before writing to database
     if ($_POST) {
         $firstName = filter_input(INPUT_POST, 'firstname', FILTER_SANITIZE_STRING);
         $lastName = filter_input(INPUT_POST, 'lastname', FILTER_SANITIZE_STRING);
         $mail = filter_input(INPUT_POST, 'mail', FILTER_VALIDATE_EMAIL);
         // Performing validation
         if (empty($firstName) || empty($lastName)) {
             $error = 'Enter your name and last name';
         } else {
             if (isset($_POST['mail']) && !empty($_POST['mail']) && $mail == FALSE) {
                 $error = 'Enter a valid email address';
             }
         }
         // Displays a message in case of errors
         if (isset($error)) {
             self::$message = '<i style="color: #f50;">ERROR: ' . $error . '</i><br /><br />';
         } else {
             // SQL insertion in JSON format
             $json = '
             insert : [
                 {
                     table: "users" ,
                     values: { firstname: "' . $firstName . '", lastname: "' . $lastName . '", mail: "' . $mail . '"  }
                 }
             ]
             ';
             // Performs the new record and store the result
             list($total) = PDO4You::execute($json);
             // Displays a success message
             self::$message = 'Register #' . $total . ' added successfully!!<br /><br />';
         }
     }
     // Capture records of all registered users
     self::$hasRecords = PDO4You::select('SELECT * FROM users ORDER BY id DESC');
 }
Exemplo n.º 4
0
 /**
  * Multiple Delete
  * 
  * */
 public function multipleDelete()
 {
     // SQL Delete in JSON format
     $json = '
     delete: [
         {
             table: "users" , 
             where: { id: 4 }
         },{
             table: "users" ,
             where: { id: 20 }
         },{
             table: "users" ,
             where: { id: 30 }
         },{
             table: "books" ,
             where: { id: 10 }
         } 
     ]
     ';
     // Executes the SQL and stores the result
     $result = PDO4You::execute($json);
     echo '<div class="code title">Demo with Delete command</div>';
     echo '<div class="code debug">PDO4You::execute(' . $this->getQuery($json) . '); ' . $this->getResult($result) . '</div>';
 }