php简单操作mysql数据库的类
程序员文章站
2022-06-18 16:26:25
本文实例讲述了php简单操作mysql数据库的类。分享给大家供大家参考。具体如下:
本文实例讲述了php简单操作mysql数据库的类。分享给大家供大家参考。具体如下:
<?php /** * database class * * @version: 2.2 * @revised: 27 may 2007 * **/ class database { var $host; var $name; var $user; var $pass; var $prefix; var $linkid; function database($mysql) { foreach($mysql as $k => $v){ $this->$k = $v; } if(strlen($this->prefix)>0 && substr($this->prefix, -1) !== "_") $prefix .= "_"; $this->prefix = $prefix; } function getlastid() { $id = mysql_fetch_row(mysql_query("select last_insert_id()", $this->linkid)); return $id[0]; } function getpossiblevalues($tablea, $wherea) { if(gettype($tablea) == "array") { $table = ""; foreach($tablea as $t) { $table .= $this->prefix.$t.", "; } $table = substr($table, 0, -2); } else $table = $this->prefix.$tablea; if(gettype($wherea) != "array") { $wherea = array($wherea); } $return = array(); foreach($wherea as $where) { $sql = mysql_query("show columns from ".$table." like '%".$where."%'"); while($arr = mysql_fetch_array($sql)) { if(strpos($arr['type'], 'enum')===0) { $vals = substr($arr['type'], 5, -1); } else { $vals = substr($arr['type'], 4, -1); } $vals = str_replace("'","",$vals); $vals = explode(",",$vals); $i = 1; foreach($vals as $val) { $return[$arr['field']][$i++] = $val; } $return[$arr['field']]['default'] = $arr['default']; if($arr['null'] != "no") $return[$arr['field']][0] = null; } } return $return; } function connect() { $this->linkid = mysql_connect($this->host, $this->user, $this->pass); if(!$this->linkid) { return false; } if(mysql_select_db($this->name, $this->linkid)) return true; mysql_close($this->linkid); return false; } function runselect($tables, $where = "1", $fieldsa = "*", $order = false, $limit = false, $offset = false, $group = false) { if(gettype($tables) == "array") { $table = ""; foreach($tables as $t) { $table .= $this->prefix.$t.", "; } $table = substr($table, 0, -2); } else $table = $this->prefix.$tables; if(gettype($fieldsa) == "array") { $fields = ""; $keys = array_keys($fieldsa); if($keys[0] != '0') { foreach($keys as $key) { $fields .= $key.' as '.$fieldsa[$key].', '; } } else { foreach($fieldsa as $field) { $fields .= $field.', '; } } $fields = substr($fields, 0, -2); } else $fields = $fieldsa; $query = "select ".$fields." from ".$table." where ".$where. ($order!== false?" order by ".$order:($group!==false ? " group by ".$group : "")). ($limit !== false?" limit ".$limit:""). ($offset !== false?" offset ".$offset:""); return mysql_query($query, $this->linkid); } function runupdate($table, $valuesa, $where = "1") { if(gettype($valuesa) == "array") { $fields = ""; $values = ""; $keys = array_keys($valuesa); foreach($keys as $key) { if($valuesa[$key] !== null) $values .= "`".$key."`='".str_replace("'",'\'', $valuesa[$key])."',"; else $values .= $key."=null,"; } $fields = substr($fields, 0, -1); $values = substr($values, 0, -1); } else $values = $valuesa; $query = "update ".$this->prefix.$table." set ".$values." where ".$where; if(mysql_query($query, $this->linkid)) return mysql_affected_rows($this->linkid); return false; } function rundelete($table, $where = "1") { if(mysql_query("delete from ".$this->prefix.$table." where ".$where, $this->linkid)) return mysql_affected_rows($this->linkid); return false; } function runinsert($table, $valuesa, $onduplicate = null) { if(gettype($valuesa) == "array") { $fields = ""; $values = ""; $keys = array_keys($valuesa); foreach($keys as $key) { $fields .= "`".$key."`, "; $values .= ($valuesa[$key]===null?"null, ":"'".str_replace("'", '\'', $valuesa[$key])."', "); } $fields = substr($fields, 0, -2); $values = substr($values, 0, -2); } $ondup = ""; if($onduplicate != null) { $ondup = " on duplicate key update "; if(gettype($onduplicate) == "array") { $keys = array_keys($onduplicate); foreach($keys as $key) { $ondup .= '`'.$key.'`='.($onduplicate[$key]===null?"null,":"'".str_replace("'", '\'', $onduplicate[$key])."', "); } $ondup = substr($ondup, 0, -2); } else $ondup .= $onduplicate; } $query = "insert into ".$this->prefix.$table.($fields!==null?"(".$fields.")":""). " values (".$values.")".$ondup; if(mysql_query($query, $this->linkid)) return mysql_affected_rows($this->linkid); return false; } function getcells($table){ $query = "show columns from `".$table."`"; $fields = mysql_query($query, $this->linkid) or die('hej'); return $fields; } function translatecellname($cellname){ $sql = $this->runselect("mysql_cell_translation","mysql_name = '".$cellname."'"); $row = mysql_fetch_assoc($sql); return $row['human_name']?$row['human_name']:'<span class="faded">['.$cellname.']</span>'; } function geterror() { return mysql_error($this->linkid); } function close() { mysql_close($this->linkid); } } ?>
希望本文所述对大家的php+mysql数据库程序设计有所帮助。