?phpheader("content-type:text/html;charset=utf-8");class DBUtils{ public function update($sql){ $link = $this->getConn(); mysql_query($sql); //如果出错显示 if(DEBUG){ echo mysql_error(); } $rs = mysql_affected_rows($link); $rs = $rs > 0; mysql_close($link); return $rs; } public function queryRows($sql){ //创建连接,编码,数据库 $link = $this->getConn(); //发送sql $rs = mysql_query($sql); //如果出错显示 if(DEBUG){ echo mysql_error(); } $rows = array(); while($row = mysql_fetch_array($rs)){ $rows[] = $row;//pdemo7.php } // mysql_free_result($rs); mysql_close($link); return $rows; } public function queryRow($sql){ $rs = $this->queryRows($sql); if(!empty($rs[0])){ return $rs[0]; } return false; } public function queryObj($sql){ $rs = $this->queryRows($sql); //var_dump($rs); if(!empty($rs[0][0])){ return $rs[0][0]; } return false; } private function getConn(){ $link = mysql_connect('127.0.0.1','root',''); mysql_query("set names utf8"); mysql_select_db("news"); return $link; } }
PHP工具类 MySQL增删改查工具类
2025-07-22 09:56 点击:0