我们不需要自己定义相关的类,可以使用PHPLIB里已经定义的类来操作数据库。PHPLIB(PHP Base Library,PHP基本库,主页:< A href="< A href="http://phpwizard.net)是PHP的扩展,它提供了很多类库,使得程序员能很容易地建立一个交互式Web站点,PHPLIB最基本的功能包括以下几个方面:(1)用户认证,(2)Session管理,(3)权限及更加方便的使用数据库。"" target="_NEW">http://phpwizard.net)是PHP的扩展,它提供了很多类库,使得程序员能很容易地建立一个交互式Web站点,PHPLIB最基本的功能包括以下几个方面:(1)用户认证,(2)Session管理,(3)权限及更加方便的使用数据库。"< /A> target="_NEW">< A href="http://phpwizard.net)是PHP的扩展,它提供了很多类库,使得程序员能很容易地建立一个交互式Web站点,PHPLIB最基本的功能包括以下几个方面:(1)用户认证,(2)Session管理,(3)权限及更加方便的使用数据库。< " target="_NEW">http://phpwizard.net)是PHP的扩展,它提供了很多类库,使得程序员能很容易地建立一个交互式Web站点,PHPLIB最基本的功能包括以下几个方面:(1)用户认证,(2)Session管理,(3)权限及更加方便的使用数据库。< < /A> /A>
下面具体介绍一下PHPLIB中针对MySQL数据库的类及其使用方法。 PHPLIB中对MySQL的类的定义在db_mysql.inc中,其源代码如下: < ?php /* 版权信息 * Session Management for PHP3 * * Copyright (c) 1998-2000 NetUSE AG * Boris Erdmann, Kristian Koehntopp * * $Id: db_mysql.inc,v 1.2 2000/07/12 18:22:34 kk Exp $ * */
class DB_Sql {
/* 数据库连接参数,改成你要连接的数据库的地址,数据库名,用户名和密码 */ var $Host = ""; var $Database = ""; var $User = ""; var $Password = "";
/* 配置参数 */ var $Auto_Free = 0; ## 设为1则自动执行mysql_free_result()函数 var $Debug = 0; ## 设为1则显示SQL语句 var $Halt_On_Error = "yes"; ## 出错时,"yes" (显示信息,中止运行), "no" (忽略错误,继续运行), "report" (忽略错误,显示警告信息) var $Seq_Table = "db_sequence";
/* 数据库查询结果,和当前的行数 */ var $Record = array(); var $Row;
/* 当前的错误号和错误信息 */ var $Errno = 0; var $Error = "";
/* public: this is an api revision, not a CVS revision. */ var $type = "mysql"; var $revision = "1.2";
/* 联接句柄和执行SQL语句的句柄 */ var $Link_ID = 0; var $Query_ID = 0;
/* 以下是类的方法 */ function DB_Sql($query = "") { $this->query($query); }
/* 返回Link_ID和Query_ID值 */ function link_id() { return $this->Link_ID; } function query_id() { return $this->Query_ID; }
/* 连接处理 */ 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;
/* 完成一个查询操作 */ 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);
/* 定位到某一条查询记录 */ 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; }
/* * Due to compatibility problems with Table we changed the behavior * of metadata(); * depending on $full, metadata returns the following values: * * - full is false (default): * $result[]: * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["flags"] field flags * * - full is true * $result[]: * ["num_fields"] number of metadata records * [0]["table"] table name * [0]["name"] field name * [0]["type"] field type * [0]["len"] field length * [0]["flags"] field flags * ["meta"][field name] index of field named "field name" * The last one is used, if you have a field name, but no index. * Test: if (isset($result['meta']['myfield'])) { ... */ // if no $table specified, assume that we are working with a query // result if ($table) { $this->connect(); $id = @mysql_list_fields($this->Database, $table); if (!$id) $this->halt("Metadata query failed."); } else { $id = $this->Query_ID; if (!$id) $this->halt("No query specified."); }
$count = @mysql_num_fields($id);
// made this IF due to performance (one if is faster than $count if's) if (!$full) { for ($i=0; $i< $count; $i++) { $res[$i]["table"] = @mysql_field_table ($id, $i); $res[$i]["name"] = @mysql_field_name ($id, $i); $res[$i]["type"] = @mysql_field_type ($id, $i); $res[$i]["len"] = @mysql_field_len ($id, $i); $res[$i]["flags"] = @mysql_field_flags ($id, $i); } } else { // full $res["num_fields"]= $count;
评论