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

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

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

其中发下如下几句:

  1. public function autoUpdate($id,$data){
  2. $where = " where {$this->fields['Key']} = '{$id}'";
  3. $sql = "update {$this->getTable()} set ";
  4. foreach($data as $key => $value){
  5. $sql .= $key . '="' . $value . '",';
  6. }
  7. $sql = rtrim($sql,',') . $where;
  8. return $this->exec($sql);
  9. }
  1. public function autoInsert($data){
  2. $keys = $values = '';
  3. foreach($this->fields as $k => $v){
  4. if($k == 'Key') continue;
  5. if(array_key_exists($v,$data)){
  6. $keys .= $v . ',';
  7. $values .= "'" . $data[$v] . "',";
  8. }
  9. }
  10. $keys = rtrim($keys,',');
  11. $values = rtrim($values,',');
  12. $sql = "insert into {$this->getTable()} ({$keys}) values({$values})";
  13. return $this->exec($sql);
  14. }

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

SQL语句如下:

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

如下:

相关文章

最新文章

更多