select() public static méthode

Method referring to the fetchAll(PDO::FETCH_ASSOC)
public static select ( string $sql, string $use = null ) : array
$sql string Instruction SQL of query of records
$use string OPTIONAL Name of the database defined as a new connection instance
Résultat array Returns an array indexed by column name
 public function testAllTypesSelects()
 {
     // SQL query
     $sql = 'SELECT * FROM users';
     // Executes the SQL and stores the result
     $result = test::select($sql);
     $this->assertEquals(1, self::getNumRows($result));
     // Executes the SQL and stores the result
     $result_num = test::selectNum($sql);
     $this->assertEquals(1, self::getNumRows($result_num));
     // Executes the SQL and stores the result
     $result_obj = test::selectObj($sql);
     $this->assertEquals(1, self::getNumRows($result_obj));
     // Executes the SQL and stores the result
     $result_all = test::selectAll($sql);
     $this->assertEquals(1, self::getNumRows($result_all));
 }
Exemple #2
0
 /**
  * All Selects
  * 
  * */
 public function allSelects()
 {
     // SQL query
     $sql = self::sql();
     // Executes the SQL and stores the result
     $result = PDO4You::select($sql);
     $result_num = PDO4You::selectNum($sql);
     $result_obj = PDO4You::selectObj($sql);
     $result_all = PDO4You::selectAll($sql);
     echo '<div class="code title">Demo with the method PDO4You::select()</div>';
     echo '<div class="code debug">PDO4You::select(' . $this->getQuery($sql) . '); ' . $this->getResult($result) . '</div>';
     echo '<div class="code title">Demo with the method PDO4You::selectNum()</div>';
     echo '<div class="code debug">PDO4You::selectNum(' . $this->getQuery($sql) . '); ' . $this->getResult($result_num) . '</div>';
     echo '<div class="code title">Demo with the method PDO4You::selectObj()</div>';
     echo '<div class="code debug">PDO4You::selectObj(' . $this->getQuery($sql) . '); ' . $this->getResult($result_obj) . '</div>';
     echo '<div class="code title">Demo with the method PDO4You::selectAll()</div>';
     echo '<div class="code debug">PDO4You::selectAll(' . $this->getQuery($sql) . '); ' . $this->getResult($result_all) . '</div>';
 }
 /**
  * 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');
 }