当前位置:编程学习 > php >>

加强版phplib的DB类

答案:
复制代码 代码如下:

<?php
/****************************************************************************************************************
为了便于自己的开发,又不想使用ADODB、PEAR::DB这样的庞然大物,
就用在PHPLib DB类的基础上、参考PEAR::DB类,封装的DB类,简单好使,非常方便。
MySQL有效

[ 连接数据库 ]

//包含数据库处理类文件
include_once("database.inc.php");

//本地数据库配置
define("DB_HOST", "localhost"); //数据库服务器
define("DB_USER_NAME", "root"); //数据库用户名
define("DB_USER_PASS", ""); //密码
define("DB_DATABASE", "test"); //数据库

//连接本地数据库
$db = new DB_Sql();
$db->connect(DB_DATABASE, DB_HOST, DB_USER_NAME, DB_USER_PASS);

[ 使用方法 ]

//获取所有记录
$sql = "SELECT * FROM table1";
$all_record = $db->get_all($sql);

//获取一条
$sql = "SELECT * FROM table1 WHERE id = '1'";
$one_row = $db->get_one($sql);

//分页查询,提取20条记录
$sql = "SELECT * FROM table1";
$page_record = $db->limit_query($sql, $start=0, $offset=20, $order="ORDER BY id DESC");

//提取指定数目的记录
$sql = "SELECT * FROM table1";
$limit_record = $db->get_limit($sql,10);

//统计记录数,统计所有类型为学生的
$count = $db->count("table1", "id", "type = 'student'");

//插入一条记录
$info_array = array(
"name" => "heiyeluren",
"type" => "student",
"age" => "22",
"gender" => "boy"
);
$db->insert("table1", $info_array);

//更新一条记录
$info_array = array(
"name" => "heiyeluren",
"type" => "teacher",
"age" => "22",
"gender" => "boy"
);
$db->update("table1", $info_array, "name = 'heiyeluren'");

//删除记录
$db->delete("table1", "name = 'heiyeluren'");

//执行一条无结果集的SQL
$db->execute("DELETE FROM table1 WHERE name = 'heiyeluren'");

********************************************************************************************************/

/**
* 文件: database.inc.php
* 描述: 数据库操作类
* 说明: 本库使用PHPLib DB库作为核心, 同时增加一些实用方法, 详细参考注释
*/

class DB_Sql
{

/* public: connection parameters */
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";

/* public: configuration parameters */
var $Auto_Free = 1; ## Set to 1 for automatic mysql_free_result()
var $Debug = 0; ## Set to 1 for debugging messages.
var $Halt_On_Error = "yes"; ## "yes" (halt with message), "no" (ignore errors quietly), "report" (ignore errror, but spit a warning)
var $PConnect = 0; ## Set to 1 to use persistent database connections
var $Seq_Table = "db_sequence";

/* public: result array and current row number */
var $Record = array();
var $Row;

/* public: current error number and error text */
var $Errno = 0;
var $Error = "";

/* public: this is an api revision, not a CVS revision. */
var $type = "mysql";
//var $revision = "1.2";

/* private: link and query handles */
var $Link_ID = 0;
var $Query_ID = 0;

var $locked = false; ## set to true while we have a lock

/* public: constructor */
function DB_Sql() {
$this->query($query);
}

/* public: some trivial reporting */
function link_id() {
return $this->Link_ID;
}

function query_id() {
return $this->Query_ID;
}

/* public: connection management */
function connect($Database = "", $Host = "", $User = "", $Password = "") {
/* Handle defaults */
if ("" == $Database)
$Database = $this->Database;

if ("" == $Host)
$Host = $this->Host;

if ("" == $User)
$User = $this->User;

if ("" == $Password)
$Password = $this->Password;


/* establish connection, select database */
if ( 0 == $this->Link_ID ) {

if(!$this->PConnect) {
$this->Link_ID = mysql_connect($Host, $User, $Password);
} else {
$this->Link_ID = mysql_pconnect($Host, $User, $Password);
}
if (!$this->Link_ID) {
$this->halt("connect($Host, $User, \$Password) failed.");
return 0;
}

if (!@mysql_select_db($Database,$this->Link_ID)) {
$this->halt("cannot use database ".$Database);
return 0;
}
}

return $this->Link_ID;
}

/* public: discard the query result */
function free() {
@mysql_free_result($this->Query_ID);
$this->Query_ID = 0;
}

/* public: perform a query */
function query($Query_String) {
/* No empty queries, please, since PHP4 chokes on them. */
if ($Query_String == "")
/* The empty query string is passed on from the constructor,
* when calling the class without a query, e.g. in situations
* like these: '$db = new DB_Sql_Subclass;'
*/
return 0;

if (!$this->connect()) {
return 0; /* we already complained in connect() about that. */
};

# New query, discard previous result.
if ($this->Query_ID) {
$this->free();
}

if ($this->Debug)
printf("Debug: query = %s<br>\n", $Query_String);

$this->Query_ID = @mysql_query($Query_String,$this->Link_ID);
$this->Row = 0;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if (!$this->Query_ID) {
$this->halt("Invalid SQL: ".$Query_String);
}

# Will return nada if it fails. That's fine.
return $this->Query_ID;
}

/* public: walk result set */
function next_record() {
if (!$this->Query_ID) {
$this->halt("next_record called with no query pending.");
return 0;
}

$this->Record = @mysql_fetch_array($this->Query_ID);
$this->Row += 1;
$this->Errno = mysql_errno();
$this->Error = mysql_error();

$stat = is_array($this->Record);
if (!$stat && $this->Auto_Free) {
$this->free();
}
return $stat;
}

/* public: position in result set */
function seek($pos = 0) {
$status = @mysql_data_seek($this->Query_ID, $pos);
if ($status)
$this->Row = $pos;
else {
$this->halt("seek($pos) failed: result has ".$this->num_rows()." rows.");

/* half assed attempt to save the day,
* but do not consider this documented or even
* desireable behaviour.
*/
@mysql_data_seek($this->Query_ID, $this->num_rows());
$this->Row = $this->num_rows();
return 0;
}

return 1;
}

/* public: table locking */
function lock($table, $mode = "write") {
$query = "lock tables ";
if(is_array($table)) {
while(list($key,$value) = each($table)) {
// text keys are "read", "read local", "write", "low priority write"
if(is_int($key)) $key = $mode;
if(strpos($value, ",")) {
$query .= str_replace(",", " $key, ", $value) . " $key, ";
} else {
$query .= "$value $key, ";
}
}

上一个:用来给图片加水印的PHP类
下一个:关于Intype一些小问题的解决办法

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,