コード例 #1
0
ファイル: Result.php プロジェクト: voku/simple-mysqli
 /**
  * Cast data into int, float or string.
  *
  * INFO: install / use "mysqlnd"-driver for better performance
  *
  * @param array|object $data
  *
  * @return array|false false on error
  */
 private function cast(&$data)
 {
     if (Helper::isMysqlndIsUsed() === true) {
         return $data;
     }
     // init
     static $fields = array();
     static $types = array();
     $result_hash = spl_object_hash($this->_result);
     if (!isset($fields[$result_hash])) {
         $fields[$result_hash] = \mysqli_fetch_fields($this->_result);
     }
     if ($fields[$result_hash] === false) {
         return false;
     }
     if (!isset($types[$result_hash])) {
         foreach ($fields[$result_hash] as $field) {
             switch ($field->type) {
                 case 3:
                     $types[$result_hash][$field->name] = 'int';
                     break;
                 case 4:
                     $types[$result_hash][$field->name] = 'float';
                     break;
                 default:
                     $types[$result_hash][$field->name] = 'string';
                     break;
             }
         }
     }
     if (is_array($data) === true) {
         foreach ($types[$result_hash] as $type_name => $type) {
             if (isset($data[$type_name])) {
                 settype($data[$type_name], $type);
             }
         }
     } elseif (is_object($data)) {
         foreach ($types[$result_hash] as $type_name => $type) {
             if (isset($data->{$type_name})) {
                 settype($data->{$type_name}, $type);
             }
         }
     }
     return $data;
 }
コード例 #2
0
ファイル: DB.php プロジェクト: voku/simple-mysqli
 /**
  * Set the current charset.
  *
  * @param string $charset
  *
  * @return bool
  */
 public function set_charset($charset)
 {
     $charsetLower = strtolower($charset);
     if ($charsetLower === 'utf8' || $charsetLower === 'utf-8') {
         $charset = 'utf8';
     }
     if ($charset === 'utf8' && Helper::isUtf8mb4Supported($this) === true) {
         $charset = 'utf8mb4';
     }
     $this->charset = (string) $charset;
     $return = mysqli_set_charset($this->link, $charset);
     /** @noinspection PhpUsageOfSilenceOperatorInspection */
     @\mysqli_query($this->link, 'SET CHARACTER SET ' . $charset);
     /** @noinspection PhpUsageOfSilenceOperatorInspection */
     @\mysqli_query($this->link, "SET NAMES '" . $charset . "'");
     return $return;
 }