Home > Database > Mysql Tutorial > php:数据库操作类

php:数据库操作类

WBOY
Release: 2016-06-07 16:37:36
Original
866 people have browsed it

在动态网页的世界里面,数据库是就是最有利、最高效的存储工具。动态类型网站,不管是只有寥寥几十篇文章的个人博客,还是部署了大量资源的门户、论坛、社区等都没有办法脱离数据库的支持。所以说,数据库是网站的一个灵魂,那是一点都不为过的。而Mysql数据

在动态网页的世界里面,数据库是就是最有利、最高效的存储工具。动态类型网站,不管是只有寥寥几十篇文章的个人博客,还是部署了大量资源的门户、论坛、社区等都没有办法脱离数据库的支持。所以说,数据库是网站的一个灵魂,那是一点都不为过的。而Mysql数据库是PHP最好的搭档,现在PHP的流行程度,我想是不言而喻的,而伴随着PHP流行的是MYsql数据库的广泛应用。因此呢,懂得PHP操作数据库那是非常重要的,要玩PHP就要懂数据库。我现在与大家分享一个mysql数据库操作的类。

php操作mysql类

<?php ??
define("db_server","localhost");? ??
define("db_user","root");? ??
define("db_password","root");? ??
class?mysql{? ??
var?$conn;? ??
var?$error;? ??
function?get_error(){? ??
????return?$this->error;? ??
} ??
function?connect($dbname)?{? ??
????$this->conn?=?mysql_connect(db_server,?db_user,?db_password);? ??
????if?(!!$this->conn){? ??
????????mysql_select_db($dbname,?$this->conn);? ??
????????return?true;? ??
????}? ??
????$this->error?=?mysql_error();? ??
????return?false;? ??
}? ??
function?insert($table,?$dataarray){? ??
????$i?=?0;?$data?=?'';? ??
????while(isset($dataarray[$i])){? ??
????????$data?.=?"'"?.?$dataarray[$i]?.?"'";? ??
????????if?(isset($dataarray[$i+1]))??$data?.=?",?";? ??
????????$i++;? ??
????}? ??
????if(mysql_query("INSERT?INTO?$table?VALUES?($data)",?$this->conn)){? ??
????????return?true;? ??
????}else{? ??
????????$this->error?=?mysql_error($this->conn);? ??
????????return?false;? ??
????}? ??
}? ??
function?query($q){? ??
????if($result?=?mysql_query($q,?$this->conn)){? ??
????????return?$result;? ??
????}else{? ??
????????$this->error=mysql_error($this->conn);? ??
????????return?false;? ??
????}? ??
}? ??
function?update($table,$fields,$values,$clause){? ??
????if?(count($fields)?!=?count($values)){? ??
????????return?false;? ??
????}? ??
????$sql?=?"UPDATE?$table?SET?";? ??
????$i=0;? ??
????while(isset($fields[$i])){? ??
????????$sql?=?$sql?.?$fields[$i]?.?"?=?'"?.?$values[$i]?.?"'";? ??
????????if?(isset($fields[$i]))?$sql?=?$sql?+?',?';? ??
????????$i++;? ??
????}? ??
????$sql?=?$sql?+?"?WHERE?$clause";? ??
????return?mysql_query($sql);? ??
}? ??
function?max($table,?$field){? ??
????$result?=?mysql_query("SELECT?MAX($field)?FROM?$table",?$this->conn);? ??
????$row?=?mysql_fetch_array($result);? ??
????return?$row["MAX($field)"];? ??
}? ??
function?getfields($table){? ??
????if($result?=?mysql_query("SHOW?COLUMNS?FROM?$table",?$this->conn)){? ??
????????$cms?=?mysql_fetch_assoc($result);? ??
????????return?$cms['Field'];? ??
????}else{? ??
????????$this->error?=?mysql_error($this->conn);? ??
????????return?false;? ??
????}? ??
}? ??
function?getrows($table,$fields,?$clause=NULL,?$order=NULL,?$llimit=NULL,?$ulimit=NULL){? ??
????$sql?=?"SELECT?";? ??
????$sql?.=?($fields?!=?'')??$fields?.?"?"?:?"*?";? ??
????$sql?.=?"FROM?$table?";? ??
????if?(isset($clause))?$sql?.=?"WHERE?$clause?";? ??
????if?($order?!=?'')?$sql?.=?"ORDER?BY?$order?";? ??
????if?(isset($clause))?$sql?.=?"LIMIT?$llimit,?$ulimit";? ??
????$sql?.=?";";? ??
????if($result?=?mysql_query($sql)){? ??
????????return?$result;? ??
????}else{? ??
????????$this->error=mysql_error($this->conn);? ??
????????return?false;? ??
????}? ??
}? ??
function?getrow($table,?$fields=NULL,?$clause=NULL){? ??
????$rows?=?$this->getrows($table,?$fields,?$clause,?'',?0,?1);? ??
????if?(mysql_num_rows($rows))? ??
????????return?mysql_fetch_array($rows);? ??
????else? ??
????????return?false;? ??
}? ??
function?nextrow($rows){? ??
????return?mysql_fetch_array($rows);? ??
}? ??
function?delete($table,?$clause){? ??
????mysql_query("DELETE?*?FROM?$table?WHERE?$clause");? ??
}? ??
function?close(){? ??
????mysql_close();? ??
}? ??
}? ??
?>??
Copy after login

这些代码,我也就不多加解释了,很基础的,不过是方便大家而已!!

声明: 本文采用 BY-NC-SA 协议进行授权 | IT路人
转载请注明转自《php:数据库操作类》

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template