{"id":140,"date":"2022-05-13T20:19:08","date_gmt":"2022-05-13T12:19:08","guid":{"rendered":"http:\/\/www.sangco.cn\/?p=140"},"modified":"2022-05-13T20:27:29","modified_gmt":"2022-05-13T12:27:29","slug":"php%e5%b0%81%e8%a3%85%e7%9a%84mysqli%e7%b1%bb%e5%ae%8c%e6%95%b4%e5%ae%9e%e4%be%8b","status":"publish","type":"post","link":"https:\/\/www.sangco.cn\/?p=140","title":{"rendered":"php\u5c01\u88c5\u7684mysqli\u7c7b\u5b8c\u6574\u5b9e\u4f8b"},"content":{"rendered":"<p>\u672c\u6587\u5b9e\u4f8b\u8bb2\u8ff0\u4e86php\u5c01\u88c5\u7684mysqli\u7c7b\u3002\u5206\u4eab\u7ed9\u5927\u5bb6\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5982\u4e0b\uff1a<\/p>\n<p>\u7c7b\uff1a<\/p>\n<pre class=\"brush:php\">&lt;?php\r\nheader('content-type:text\/html;charset=utf-8');\r\n\/*\r\n\u638c\u63e1\u6ee1\u8db3\u5355\u4f8b\u6a21\u5f0f\u7684\u5fc5\u8981\u6761\u4ef6\r\n(1)\u79c1\u6709\u7684\u6784\u9020\u65b9\u6cd5-\u4e3a\u4e86\u9632\u6b62\u5728\u7c7b\u5916\u4f7f\u7528new\u5173\u952e\u5b57\u5b9e\u4f8b\u5316\u5bf9\u8c61\r\n(2)\u79c1\u6709\u7684\u6210\u5458\u5c5e\u6027-\u4e3a\u4e86\u9632\u6b62\u5728\u7c7b\u5916\u5f15\u5165\u8fd9\u4e2a\u5b58\u653e\u5bf9\u8c61\u7684\u5c5e\u6027\r\n(3)\u79c1\u6709\u7684\u514b\u9686\u65b9\u6cd5-\u4e3a\u4e86\u9632\u6b62\u5728\u7c7b\u5916\u901a\u8fc7clone\u6210\u751f\u53e6\u4e00\u4e2a\u5bf9\u8c61\r\n(4)\u516c\u6709\u7684\u9759\u6001\u65b9\u6cd5-\u4e3a\u4e86\u8ba9\u7528\u6237\u8fdb\u884c\u5b9e\u4f8b\u5316\u5bf9\u8c61\u7684\u64cd\u4f5c\r\n*\/\r\nclass\u00a0ConnectMysqli{\r\n\u00a0\u00a0\/\/\u79c1\u6709\u7684\u5c5e\u6027\r\n\u00a0\u00a0private\u00a0static\u00a0$dbcon=false;\r\n\u00a0\u00a0private\u00a0$host;\r\n\u00a0\u00a0private\u00a0$port;\r\n\u00a0\u00a0private\u00a0$user;\r\n\u00a0\u00a0private\u00a0$pass;\r\n\u00a0\u00a0private\u00a0$db;\r\n\u00a0\u00a0private\u00a0$charset;\r\n\u00a0\u00a0private\u00a0$link;\r\n\u00a0\u00a0\/\/\u79c1\u6709\u7684\u6784\u9020\u65b9\u6cd5\r\n\u00a0\u00a0private\u00a0function\u00a0__construct($config=array()){\r\n\u00a0\u00a0\u00a0\u00a0$this-&gt;host =\u00a0$config['host'] ?\u00a0$config['host'] :\u00a0'localhost';\r\n\u00a0\u00a0\u00a0\u00a0$this-&gt;port =\u00a0$config['port'] ?\u00a0$config['port'] :\u00a0'3306';\r\n\u00a0\u00a0\u00a0\u00a0$this-&gt;user =\u00a0$config['user'] ?\u00a0$config['user'] :\u00a0'root';\r\n\u00a0\u00a0\u00a0\u00a0$this-&gt;pass =\u00a0$config['pass'] ?\u00a0$config['pass'] :\u00a0'root';\r\n\u00a0\u00a0\u00a0\u00a0$this-&gt;db =\u00a0$config['db'] ?\u00a0$config['db'] :\u00a0'small2';\r\n\u00a0\u00a0\u00a0\u00a0$this-&gt;charset=isset($arr['charset']) ?\u00a0$arr['charset'] :\u00a0'utf8';\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u8fde\u63a5\u6570\u636e\u5e93\r\n\u00a0\u00a0\u00a0\u00a0$this-&gt;db_connect();\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u9009\u62e9\u6570\u636e\u5e93\r\n\u00a0\u00a0\u00a0\u00a0$this-&gt;db_usedb();\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u8bbe\u7f6e\u5b57\u7b26\u96c6\r\n\u00a0\u00a0\u00a0\u00a0$this-&gt;db_charset();\r\n\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\/\/\u8fde\u63a5\u6570\u636e\u5e93\r\n\u00a0\u00a0\u00a0private\u00a0function\u00a0db_connect(){\r\n\u00a0\u00a0\u00a0\u00a0$this-&gt;link=mysqli_connect($this-&gt;host.':'.$this-&gt;port,$this-&gt;user,$this-&gt;pass);\r\n\u00a0\u00a0\u00a0\u00a0if(!$this-&gt;link){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo\u00a0\"\u6570\u636e\u5e93\u8fde\u63a5\u5931\u8d25&lt;br&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo\u00a0\"\u9519\u8bef\u7f16\u7801\".mysqli_errno($this-&gt;link).\"&lt;br&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo\u00a0\"\u9519\u8bef\u4fe1\u606f\".mysqli_error($this-&gt;link).\"&lt;br&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0exit;\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\/\/\u8bbe\u7f6e\u5b57\u7b26\u96c6\r\n\u00a0\u00a0\u00a0\u00a0private\u00a0function\u00a0db_charset(){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0mysqli_query($this-&gt;link,\"set names {$this-&gt;charset}\");\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u9009\u62e9\u6570\u636e\u5e93\r\n\u00a0\u00a0\u00a0private\u00a0function\u00a0db_usedb(){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0mysqli_query($this-&gt;link,\"use {$this-&gt;db}\");\r\n\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\/\/\u79c1\u6709\u7684\u514b\u9686\r\n\u00a0\u00a0\u00a0private\u00a0function\u00a0__clone(){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0die('clone is not allowed');\r\n\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\/\/\u516c\u7528\u7684\u9759\u6001\u65b9\u6cd5\r\n\u00a0\u00a0\u00a0public\u00a0static\u00a0function\u00a0getIntance(){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0if(self::$dbcon==false){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0self::$dbcon=new\u00a0self;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0self::$dbcon;\r\n\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\/\/\u6267\u884csql\u8bed\u53e5\u7684\u65b9\u6cd5\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0query($sql){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$res=mysqli_query($this-&gt;link,$sql);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0if(!$res){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo\u00a0\"sql\u8bed\u53e5\u6267\u884c\u5931\u8d25&lt;br&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo\u00a0\"\u9519\u8bef\u7f16\u7801\u662f\".mysqli_errno($this-&gt;link).\"&lt;br&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo\u00a0\"\u9519\u8bef\u4fe1\u606f\u662f\".mysqli_error($this-&gt;link).\"&lt;br&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0$res;\r\n\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\/\/\u6253\u5370\u6570\u636e\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0p($arr){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo\u00a0\"&lt;pre&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0print_r($arr);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo\u00a0\"&lt;\/pre&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0v($arr){\r\n\u00a0\u00a0\u00a0\u00a0echo\u00a0\"&lt;pre&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0var_dump($arr);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0echo\u00a0\"&lt;\/pre&gt;\";\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u83b7\u5f97\u6700\u540e\u4e00\u6761\u8bb0\u5f55id\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0getInsertid(){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0mysqli_insert_id($this-&gt;link);\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\/**\r\n\u00a0\u00a0\u00a0\u00a0* \u67e5\u8be2\u67d0\u4e2a\u5b57\u6bb5\r\n\u00a0\u00a0\u00a0\u00a0* @param\r\n\u00a0\u00a0\u00a0\u00a0* @return string or int\r\n\u00a0\u00a0\u00a0\u00a0*\/\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0getOne($sql){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$query=$this-&gt;query($sql);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0mysqli_free_result($query);\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u83b7\u53d6\u4e00\u884c\u8bb0\u5f55,return array \u4e00\u7ef4\u6570\u7ec4\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0getRow($sql,$type=\"assoc\"){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$query=$this-&gt;query($sql);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0if(!in_array($type,array(\"assoc\",'array',\"row\"))){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0die(\"mysqli_query error\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$funcname=\"mysqli_fetch_\".$type;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0$funcname($query);\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u83b7\u53d6\u4e00\u6761\u8bb0\u5f55,\u524d\u7f6e\u6761\u4ef6\u901a\u8fc7\u8d44\u6e90\u83b7\u53d6\u4e00\u6761\u8bb0\u5f55\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0getFormSource($query,$type=\"assoc\"){\r\n\u00a0\u00a0\u00a0\u00a0if(!in_array($type,array(\"assoc\",\"array\",\"row\")))\r\n\u00a0\u00a0\u00a0\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0die(\"mysqli_query error\");\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0$funcname=\"mysqli_fetch_\".$type;\r\n\u00a0\u00a0\u00a0\u00a0return\u00a0$funcname($query);\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u83b7\u53d6\u591a\u6761\u6570\u636e\uff0c\u4e8c\u7ef4\u6570\u7ec4\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0getAll($sql){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$query=$this-&gt;query($sql);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$list=array();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0while\u00a0($r=$this-&gt;getFormSource($query)) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$list[]=$r;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0$list;\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\/**\r\n\u00a0\u00a0\u00a0\u00a0\u00a0* \u5b9a\u4e49\u6dfb\u52a0\u6570\u636e\u7684\u65b9\u6cd5\r\n\u00a0\u00a0\u00a0\u00a0\u00a0* @param string $table \u8868\u540d\r\n\u00a0\u00a0\u00a0\u00a0\u00a0* @param string orarray $data [\u6570\u636e]\r\n\u00a0\u00a0\u00a0\u00a0\u00a0* @return int \u6700\u65b0\u6dfb\u52a0\u7684id\r\n\u00a0\u00a0\u00a0\u00a0\u00a0*\/\r\n\u00a0\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0insert($table,$data){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u904d\u5386\u6570\u7ec4\uff0c\u5f97\u5230\u6bcf\u4e00\u4e2a\u5b57\u6bb5\u548c\u5b57\u6bb5\u7684\u503c\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$key_str='';\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$v_str='';\r\n\u00a0\u00a0\u00a0\u00a0\u00a0foreach($data\u00a0as\u00a0$key=&gt;$v){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(empty($v)){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0die(\"error\");\r\n\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/$key\u7684\u503c\u662f\u6bcf\u4e00\u4e2a\u5b57\u6bb5s\u4e00\u4e2a\u5b57\u6bb5\u6240\u5bf9\u5e94\u7684\u503c\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$key_str.=$key.',';\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$v_str.=\"'$v',\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$key_str=trim($key_str,',');\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$v_str=trim($v_str,',');\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u5224\u65ad\u6570\u636e\u662f\u5426\u4e3a\u7a7a\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$sql=\"insert into $table ($key_str) values ($v_str)\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;query($sql);\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u8fd4\u56de\u4e0a\u4e00\u6b21\u589e\u52a0\u64cd\u505a\u4ea7\u751fID\u503c\r\n\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0$this-&gt;getInsertid();\r\n\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\/*\r\n\u00a0\u00a0\u00a0\u00a0* \u5220\u9664\u4e00\u6761\u6570\u636e\u65b9\u6cd5\r\n\u00a0\u00a0\u00a0\u00a0* @param1 $table, $where=array('id'=&gt;'1') \u8868\u540d \u6761\u4ef6\r\n\u00a0\u00a0\u00a0\u00a0* @return \u53d7\u5f71\u54cd\u7684\u884c\u6570\r\n\u00a0\u00a0\u00a0\u00a0*\/\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0deleteOne($table,\u00a0$where){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(is_array($where)){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0foreach\u00a0($where\u00a0as\u00a0$key\u00a0=&gt;\u00a0$val) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$condition\u00a0=\u00a0$key.'='.$val;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0else\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$condition\u00a0=\u00a0$where;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$sql\u00a0=\u00a0\"delete from $table where $condition\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;query($sql);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u8fd4\u56de\u53d7\u5f71\u54cd\u7684\u884c\u6570\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0mysqli_affected_rows($this-&gt;link);\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\/*\r\n\u00a0\u00a0\u00a0\u00a0* \u5220\u9664\u591a\u6761\u6570\u636e\u65b9\u6cd5\r\n\u00a0\u00a0\u00a0\u00a0* @param1 $table, $where \u8868\u540d \u6761\u4ef6\r\n\u00a0\u00a0\u00a0\u00a0* @return \u53d7\u5f71\u54cd\u7684\u884c\u6570\r\n\u00a0\u00a0\u00a0\u00a0*\/\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0function\u00a0deleteAll($table,\u00a0$where){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(is_array($where)){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0foreach\u00a0($where\u00a0as\u00a0$key\u00a0=&gt;\u00a0$val) {\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if(is_array($val)){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$condition\u00a0=\u00a0$key.' in ('.implode(',',\u00a0$val) .')';\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0else\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$condition\u00a0=\u00a0$key.\u00a0'='\u00a0.$val;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\u00a0else\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$condition\u00a0=\u00a0$where;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$sql\u00a0=\u00a0\"delete from $table where $condition\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0$this-&gt;query($sql);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u8fd4\u56de\u53d7\u5f71\u54cd\u7684\u884c\u6570\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0mysqli_affected_rows($this-&gt;link);\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\/**\r\n\u00a0\u00a0\u00a0\u00a0* [\u4fee\u6539\u64cd\u4f5cdescription]\r\n\u00a0\u00a0\u00a0\u00a0* @param [type] $table [\u8868\u540d]\r\n\u00a0\u00a0\u00a0\u00a0* @param [type] $data [\u6570\u636e]\r\n\u00a0\u00a0\u00a0\u00a0* @param [type] $where [\u6761\u4ef6]\r\n\u00a0\u00a0\u00a0\u00a0* @return [type]\r\n\u00a0\u00a0\u00a0\u00a0*\/\r\n\u00a0\u00a0\u00a0public\u00a0function\u00a0update($table,$data,$where){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u904d\u5386\u6570\u7ec4\uff0c\u5f97\u5230\u6bcf\u4e00\u4e2a\u5b57\u6bb5\u548c\u5b57\u6bb5\u7684\u503c\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$str='';\r\n\u00a0\u00a0\u00a0\u00a0foreach($data\u00a0as\u00a0$key=&gt;$v){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0$str.=\"$key='$v',\";\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0$str=rtrim($str,',');\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u4fee\u6539SQL\u8bed\u53e5\r\n\u00a0\u00a0\u00a0\u00a0$sql=\"update $table set $str where $where\";\r\n\u00a0\u00a0\u00a0\u00a0$this-&gt;query($sql);\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u8fd4\u56de\u53d7\u5f71\u54cd\u7684\u884c\u6570\r\n\u00a0\u00a0\u00a0\u00a0return\u00a0mysqli_affected_rows($this-&gt;link);\r\n\u00a0\u00a0\u00a0}\r\n}\r\n?&gt;<\/pre>\n<p>&nbsp;<\/p>\n<p><span style=\"background-color: #ffffff; color: #555555; font-family: 'microsoft yahei'; font-size: 15px;\">\u7528\u6cd5\u6d4b\u8bd5\uff1a<\/span><\/p>\n<pre class=\"brush:php\">\/\/mysqli\u6d4b\u8bd5\r\n$db=ConnectMysqli::getIntance();\r\n\/\/var_dump($db);\r\n\/*$sql=\"select * from acticle\";\r\n$list=$db-&gt;getAll($sql);\r\n$db-&gt;p($list);*\/\r\n\/*$sql=\"select * from acticle where acticle_id=95\";\r\n$list=$db-&gt;getRow($sql);\r\n$db-&gt;p($list);\r\n*\/\r\n\/*$sql=\"select title from acticle\";\r\n$list=$db-&gt;getOne($sql);\r\n$db-&gt;p($list);\u4e0d\u884c*\/\r\n\/\/$list=$db-&gt;insert(\"users\",$_POST);\r\n\/\/$del=$db-&gt;deleteOne(\"users\",\"id=29\");\r\n\/\/$del=$db-&gt;deleteAll(\"users\",\"id in(27,28)\");\r\n\/\/$up=$db-&gt;update(\"users\",$_POST,\"id=27\");\r\n\/\/print_R($list);<\/pre>\n<p>&nbsp;<\/p>\n<p>\u5e0c\u671b\u672c\u6587\u6240\u8ff0\u5bf9\u5927\u5bb6PHP\u7a0b\u5e8f\u8bbe\u8ba1\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5b9e\u4f8b\u8bb2\u8ff0\u4e86php\u5c01\u88c5\u7684mysqli\u7c7b\u3002\u5206\u4eab\u7ed9\u5927\u5bb6\u4f9b\u5927\u5bb6\u53c2\u8003\uff0c\u5177\u4f53\u5982\u4e0b\uff1a \u7c7b\uff1a &lt;?php header(&#8216;content-type:text\/html;charset=utf-8&#8217;); \/* \u638c\u63e1\u6ee1\u8db3\u5355\u4f8b\u6a21\u5f0f\u7684\u5fc5\u8981\u6761\u4ef6 (1)\u79c1\u6709&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"topic":[],"class_list":["post-140","post","type-post","status-publish","format-standard","hentry","category-php"],"_links":{"self":[{"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/posts\/140","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=140"}],"version-history":[{"count":8,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/posts\/140\/revisions"}],"predecessor-version":[{"id":148,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=\/wp\/v2\/posts\/140\/revisions\/148"}],"wp:attachment":[{"href":"https:\/\/www.sangco.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=140"},{"taxonomy":"topic","embeddable":true,"href":"https:\/\/www.sangco.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftopic&post=140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}