MySQL笔记-SQL语句中可以使用单引号包裹任意类型

x33g5p2x  于2022-01-11 转载在 Mysql  
字(0.8k)|赞(0)|评价(0)|浏览(228)

最近看了个大佬写的PHP项目,在此膜拜下。

其中发下如下几句:

public function autoUpdate($id,$data){
        $where  = " where {$this->fields['Key']} = '{$id}'";
        $sql = "update {$this->getTable()} set ";
        foreach($data as $key => $value){
            $sql .= $key . '="' . $value . '",';
        }
        $sql = rtrim($sql,',') . $where;
        return $this->exec($sql);
    }
public function autoInsert($data){
        $keys = $values = '';
        foreach($this->fields as $k => $v){
            if($k == 'Key') continue;
            if(array_key_exists($v,$data)){
                $keys .= $v . ',';
                $values .= "'" . $data[$v] . "',";
            }
        }
        $keys = rtrim($keys,',');
        $values = rtrim($values,',');
        $sql = "insert into {$this->getTable()} ({$keys}) values({$values})";
        return $this->exec($sql);
    }

从中可以知道,连int型,就可以使用单引号,赋值。在此测试了下,如下表:

SQL语句如下:

INSERT INTO b_test(b_int, b_float, b_bInt, b_tInt, b_string) VALUES('1', '1.1', '111', '1', 'hello111')

如下:

相关文章

微信公众号

最新文章

更多