日期:2014-05-16  浏览次数:20505 次

php数据库操作类代码(增,删,改,查)

数据库操纵基本流程为:

  1、连接数据库服务器

  2、选择数据库

  3、执行SQL语句

  4、处理结果集

  5、打印操作信息

  其中用到的相关函数有

?resource mysql_connect ( [string server [, string username [, string password [, bool new_link [, int client_flags]]]]] )  连接数据库服务器
?resource mysql_pconnect ( [string server [, string username [, string password [, int client_flags]]]] )  连接数据库服务器,长连接
?int mysql_affected_rows ( [resource link_identifier] )取得最近一次与 link_identifier 关联的 INSERT,UPDATE 或 DELETE 查询所影响的记录行数。
?bool mysql_close ( [resource link_identifier] )如果成功则返回 TRUE,失败则返回 FALSE。
?int mysql_errno ( [resource link_identifier] )返回上一个 MySQL 函数的错误号码,如果没有出错则返回 0(零)。
?string mysql_error ( [resource link_identifier] )返回上一个 MySQL 函数的错误文本,如果没有出错则返回 ''(空字符串)。如果没有指定连接资源号,则使用上一个成功打开的连接从 MySQL 服务器提取错误信息。
?array mysql_fetch_array ( resource result [, int result_type] )返回根据从结果集取得的行生成的数组,如果没有更多行则返回 FALSE。
?bool mysql_free_result ( resource result )释放所有与结果标识符 result 所关联的内存。
?int mysql_num_fields ( resource result )返回结果集中字段的数目。
?int mysql_num_rows ( resource result )返回结果集中行的数目。此命令仅对 SELECT 语句有效。要取得被 INSERT,UPDATE 或者 DELETE 查询所影响到的行的数目,用 mysql_affected_rows()。
?resource mysql_query ( string query [, resource link_identifier] ) 向与指定的连接标识符关联的服务器中的当前活动数据库发送一条查询。php如果没有指定 link_identifier,则使用上一个打开的连接。如果没有打开的连接,本函数会尝试无参数调用 mysql_connect() 函数来建立一个连接并使用之。查询结果会被缓存
代码如下:来源php教程中心


. 代码如下:

class mysql {

private $db_host; //数据库主机
private $db_user; //数据库登陆名
private $db_pwd; //数据库登陆密码
private $db_name; //数据库名
private $db_charset; //数据库字符编码
private $db_pconn; //长连接标识位
private $debug; //调试开启
private $conn; //数据库连接标识
private $msg = ""; //数据库操纵信息

// private $sql = ""; //待执行的SQL语句

public function __construct($db_host, $db_user, $db_pwd, $db_name, $db_chaeset = 'utf8', $db_pconn = false, $debug = false) {
$this->db_host = $db_host;
$this->db_user = $db_user;
$this->db_pwd = $db_pwd;
$this->db_name = $db_name;
$this->db_charset = $db_chaeset;
$this->db_pconn = $db_pconn;
$this->result = '';
$this->debug = $debug;
$this->initConnect();
}

public function initConnect() {
if ($this->db_pconn) {
$this->conn = @mysql_pconnect($this->db_host, $this->db_user, $this->db_pwd);
} else {
$this->conn = @mysql_connect($this->db_host, $this->db_user, $this->db_pwd);
}
if ($this->conn) {
$this->query("SET NAMES " . $this->db_charset);
} else {
$this->msg = "数据库连接出错,错误编号:" . mysql_errno() . "错误原因:" . mysql_error();
}
$this->selectDb($this->db_name);
}

public function selectDb($dbname) {
if ($dbname == "") {
$this->db_name = $dbname;
}
if (!mysql_select_db($this->db_name, $this->conn)) {
$this->msg = "数据库不可用";
}
}

public function query($sql, $debug = false) {
if (!$debug) {
$this->result = @mysql_query($sql, $this->conn);
} else {

}
if ($this->result == false) {
$this->msg = "sql执行出错,错误编号:" . mysql_errno() . "错误原因:" . mysql_error();
}
// var_dump($this->result);
}

public function select($tableName, $columnName = "*", $where = "") {
$sql = "SELECT " . $columnName . " FROM " . $tableName;
$sql .= $where ? " WHERE " . $where : null;
$this->query($sql);
}

public function findAll($tableName) {
$sql = "SELECT * FROM $tableName";
$this->query($sql);
}

public function insert($tableName, $column = array()) {
$columnName = "";
$columnValue = "";
f