db::values PHP Method

values() static public method

Makes it possible to use arrays for inputs instead of MySQL strings
static public values ( array $input ) : string
$input array
return string The final MySQL string, which will be used in the queries.
    static function values($input)
    {
        if (!is_array($input)) {
            return $input;
        }
        $output = array();
        foreach ($input as $key => $value) {
            if ($value === 'NOW()') {
                $output[] = $key . ' = NOW()';
            } elseif (is_array($value)) {
                $output[] = $key . ' = \'' . a::json($value) . '\'';
            } else {
                $output[] = $key . ' = \'' . self::escape($value) . '\'';
            }
        }
        return implode(', ', $output);
    }

Usage Example

Exemplo n.º 1
0
Arquivo: db.php Projeto: rigidus/ea
 function smartInsert($array)
 {
     self::getColumns();
     $collect = array();
     self::$values = '';
     foreach (self::$columns as $k => $v) {
         if (isset($array[$k])) {
             $v['value'] = $array[$k];
         } else {
             if (!empty($v['default'])) {
                 $v['value'] = $v['default'];
             }
         }
         if ($v['value'] === true) {
             $v['value'] = 1;
         } else {
             if ($v['value'] === false) {
                 $v['value'] = 0;
             }
         }
         if (!preg_match("/^([A-Z]+)\\(([\\w\\W]*?)\\)\$/", $v['value'])) {
             $collect[] = "'" . addslashes($v['value']) . "'";
         } else {
             $collect[] = $v['value'];
         }
     }
     self::$values = implode(', ', $collect);
     self::$sql = "INSERT `" . self::$table . "` VALUES (" . self::$values . "); ";
     self::query(self::$sql);
 }