小弟刚学PHP不久 对以下的代码理解有困难,请大虾们解释一下,为下一代的PHP程序员做点贡献,感激不尽!!!请新手们一定要来好好学习---<?php
class DB_Sql {
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";
var $Auto_Free = 0;
var $Debug = 0;
var $Halt_On_Error = "yes";
var $PConnect = 0;
var $Seq_Table = "db_sequence";
var $Record = array();---1.用来做什么??
var $Row;
var $Errno = 0;
var $Error = "";
var $type = "mysql";--2.为什么要这样写??
var $revision = "2.11";
var $Link_ID = 0;
var $Query_ID = 0;
var $locked = false;
function DB_Sql($query = "") {
$this->query($query);----3.类中没有$query这属性 为什么能$this->query这样写??
}
function link_id() {
return $this->Link_ID;
}
function query_id() {
return $this->Query_ID;
}
function connect($Database = "", $Host = "", $User = "", $Password = "") {
if ("" == $Database)
$Database = $this->Database;
if ("" == $Host)
$Host = $this->Host;
if ("" == $User)
$User = $this->User;
if ("" == $Password)
$Password = $this->Password;
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.");---4.hait()的功能,简单问以下可不解释?
return 0;
}
if (!@mysql_select_db($Database,$this->Link_ID)) {
$this->halt("cannot use database ".$Database);
return 0;
}
}
return $this->Link_ID;
}
function free() {---------------------------------
@mysql_free_result($this->Query_ID);----5.请解释
$this->Query_ID = 0;-------------------
}
function query($Query_String) {---6.括号里面的参数???
if ($Query_String == "")
return 0;
if (!$this->connect()) {
return 0;
};
if ($this->Query_ID) {---7.为什么还要这样做呢??
$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);
}
return $this->Query_ID;
}
function next_record() {---------------8该函数的功能???
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; ------9.为什么要这样写??
$this->Errno = mysql_errno();
$this->Error = mysql_error();
$stat = is_array($this->Record);--------------10.--
if (!$stat && $this->Auto_Free) { 请说明一下??
$this->free();------------------------
}
return $stat;
}
function seek($pos = 0) {-----------------------------------------11.该函数的功能??
$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.");
@mysql_data_seek($this->Query_ID, $this->num_rows());
$this->Row = $this->num_rows();
return 0;
}----------------------------------------------------------- return 1;
}
function lock($table, $mode = "write") {------------------------------------12.请解释一下
$query = "lock tables ";
if(is_array($table)) {
while(list($key,$value) = each($table)) {
if(is_int($key)) $key = $mode;
if(strpos($value, ",")) {
$query .= str_replace(",", " $key, ", $value) . " $key, ";
} else {
$query .= "$value $key, ";
}
}
$query = substr($query, 0, -2);
} elseif(strpos($table, ",")) {
$query .= str_replace(",", " $mode, ", $table) . " $mode";
} else {
$query .= "$table $mode";
}
if(!$this->query($query)) {
$this->halt("lock() failed.");
return false;
}
$this->locked = true;
return true;------------------------------------------------------------
}
function unlock() {--------------------!!!请为以下的每个函数解释一下!!! 以下的我觉得真混淆了 不好理解!
$this->locked = false;
if(!$this->query("unlock tables")) {
$this->halt("unlock() failed.");
return false;
}
return true;
}
function affected_rows() {
return @mysql_affected_rows($this->Link_ID);
}
function num_rows() {
return @mysql_num_rows($this->Query_ID);
}
function num_fields() {
return @mysql_num_fields($this->Query_ID);
}
function nf() {
return $this->num_rows();
}
function np() {
print $this->num_rows();
}
function f($Name) {
if (isset($this->Record[$Name])) {
return $this->Record[$Name];
}
}
function p($Name) {
if (isset($this->Record[$Name])) {
print $this->Record[$Name];
}
}
function nextid($seq_name) {
if(!$this->locked) {
if($this->lock($this->Seq_Table)) {
$locked = true;
} else {
$this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
return 0;
}
}
$q = sprintf("select nextid from %s where seq_name = '%s'",
$this->Seq_Table,
$seq_name);
if(!$this->query($q)) {
$this->halt('query failed in nextid: '.$q);
return 0;
}
if(!$this->next_record()) {
$currentid = 0;
$q = sprintf("insert into %s values('%s', %s)",
$this->Seq_Table,
$seq_name,
$currentid);
if(!$this->query($q)) {
$this->halt('query failed in nextid: '.$q);
return 0;
}
} else {
$currentid = $this->f("nextid");
}
$nextid = $currentid + 1;
$q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
$this->Seq_Table,
$nextid,
$seq_name);
if(!$this->query($q)) {
$this->halt('query failed in nextid: '.$q);
return 0;
}
if($locked) {
$this->unlock();
}
return $nextid;
}
function metadata($table = "", $full = false) {
$count = 0;
$id = 0;
$res = array();
if ($table) {
$this->connect();
$id = @mysql_list_fields($this->Database, $table);
if (!$id) {
$this->halt("Metadata query failed.");
return false;
}
} else {
$id = $this->Query_ID;
if (!$id) {
$this->halt("No query specified.");
return false;
}
}
$count = @mysql_num_fields($id);
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;
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);
$res["meta"][$res[$i]["name"]] = $i;
}
}
if ($table) {
@mysql_free_result($id);
}
return $res;
}
function table_names() {
$this->connect();
$h = @mysql_query("show tables", $this->Link_ID);
$i = 0;
while ($info = @mysql_fetch_row($h)) {
$return[$i]["table_name"] = $info[0];
$return[$i]["tablespace_name"] = $this->Database;
$return[$i]["database"] = $this->Database;
$i++;
}
@mysql_free_result($h);
return $return;
}
function halt($msg) {
$this->Error = @mysql_error($this->Link_ID);
$this->Errno = @mysql_errno($this->Link_ID);
if ($this->locked) {
$this->unlock();
}
if ($this->Halt_On_Error == "no")
return;
$this->haltmsg($msg);
if ($this->Halt_On_Error != "report")
die("Session halted.");
}
function haltmsg($msg) {
printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
printf("<b>MySQL Error</b>: %s (%s)<br>\n",
$this->Errno,
$this->Error);-----------------
}
}
?>---------!!!非常感谢你的解释 我会好好学习的!!!------随便问一下有没有PHP学习交流QQ群???
class DB_Sql {
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";
var $Auto_Free = 0;
var $Debug = 0;
var $Halt_On_Error = "yes";
var $PConnect = 0;
var $Seq_Table = "db_sequence";
var $Record = array();---1.用来做什么??
var $Row;
var $Errno = 0;
var $Error = "";
var $type = "mysql";--2.为什么要这样写??
var $revision = "2.11";
var $Link_ID = 0;
var $Query_ID = 0;
var $locked = false;
function DB_Sql($query = "") {
$this->query($query);----3.类中没有$query这属性 为什么能$this->query这样写??
}
function link_id() {
return $this->Link_ID;
}
function query_id() {
return $this->Query_ID;
}
function connect($Database = "", $Host = "", $User = "", $Password = "") {
if ("" == $Database)
$Database = $this->Database;
if ("" == $Host)
$Host = $this->Host;
if ("" == $User)
$User = $this->User;
if ("" == $Password)
$Password = $this->Password;
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.");---4.hait()的功能,简单问以下可不解释?
return 0;
}
if (!@mysql_select_db($Database,$this->Link_ID)) {
$this->halt("cannot use database ".$Database);
return 0;
}
}
return $this->Link_ID;
}
function free() {---------------------------------
@mysql_free_result($this->Query_ID);----5.请解释
$this->Query_ID = 0;-------------------
}
function query($Query_String) {---6.括号里面的参数???
if ($Query_String == "")
return 0;
if (!$this->connect()) {
return 0;
};
if ($this->Query_ID) {---7.为什么还要这样做呢??
$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);
}
return $this->Query_ID;
}
function next_record() {---------------8该函数的功能???
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; ------9.为什么要这样写??
$this->Errno = mysql_errno();
$this->Error = mysql_error();
$stat = is_array($this->Record);--------------10.--
if (!$stat && $this->Auto_Free) { 请说明一下??
$this->free();------------------------
}
return $stat;
}
function seek($pos = 0) {-----------------------------------------11.该函数的功能??
$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.");
@mysql_data_seek($this->Query_ID, $this->num_rows());
$this->Row = $this->num_rows();
return 0;
}----------------------------------------------------------- return 1;
}
function lock($table, $mode = "write") {------------------------------------12.请解释一下
$query = "lock tables ";
if(is_array($table)) {
while(list($key,$value) = each($table)) {
if(is_int($key)) $key = $mode;
if(strpos($value, ",")) {
$query .= str_replace(",", " $key, ", $value) . " $key, ";
} else {
$query .= "$value $key, ";
}
}
$query = substr($query, 0, -2);
} elseif(strpos($table, ",")) {
$query .= str_replace(",", " $mode, ", $table) . " $mode";
} else {
$query .= "$table $mode";
}
if(!$this->query($query)) {
$this->halt("lock() failed.");
return false;
}
$this->locked = true;
return true;------------------------------------------------------------
}
function unlock() {--------------------!!!请为以下的每个函数解释一下!!! 以下的我觉得真混淆了 不好理解!
$this->locked = false;
if(!$this->query("unlock tables")) {
$this->halt("unlock() failed.");
return false;
}
return true;
}
function affected_rows() {
return @mysql_affected_rows($this->Link_ID);
}
function num_rows() {
return @mysql_num_rows($this->Query_ID);
}
function num_fields() {
return @mysql_num_fields($this->Query_ID);
}
function nf() {
return $this->num_rows();
}
function np() {
print $this->num_rows();
}
function f($Name) {
if (isset($this->Record[$Name])) {
return $this->Record[$Name];
}
}
function p($Name) {
if (isset($this->Record[$Name])) {
print $this->Record[$Name];
}
}
function nextid($seq_name) {
if(!$this->locked) {
if($this->lock($this->Seq_Table)) {
$locked = true;
} else {
$this->halt("cannot lock ".$this->Seq_Table." - has it been created?");
return 0;
}
}
$q = sprintf("select nextid from %s where seq_name = '%s'",
$this->Seq_Table,
$seq_name);
if(!$this->query($q)) {
$this->halt('query failed in nextid: '.$q);
return 0;
}
if(!$this->next_record()) {
$currentid = 0;
$q = sprintf("insert into %s values('%s', %s)",
$this->Seq_Table,
$seq_name,
$currentid);
if(!$this->query($q)) {
$this->halt('query failed in nextid: '.$q);
return 0;
}
} else {
$currentid = $this->f("nextid");
}
$nextid = $currentid + 1;
$q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",
$this->Seq_Table,
$nextid,
$seq_name);
if(!$this->query($q)) {
$this->halt('query failed in nextid: '.$q);
return 0;
}
if($locked) {
$this->unlock();
}
return $nextid;
}
function metadata($table = "", $full = false) {
$count = 0;
$id = 0;
$res = array();
if ($table) {
$this->connect();
$id = @mysql_list_fields($this->Database, $table);
if (!$id) {
$this->halt("Metadata query failed.");
return false;
}
} else {
$id = $this->Query_ID;
if (!$id) {
$this->halt("No query specified.");
return false;
}
}
$count = @mysql_num_fields($id);
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;
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);
$res["meta"][$res[$i]["name"]] = $i;
}
}
if ($table) {
@mysql_free_result($id);
}
return $res;
}
function table_names() {
$this->connect();
$h = @mysql_query("show tables", $this->Link_ID);
$i = 0;
while ($info = @mysql_fetch_row($h)) {
$return[$i]["table_name"] = $info[0];
$return[$i]["tablespace_name"] = $this->Database;
$return[$i]["database"] = $this->Database;
$i++;
}
@mysql_free_result($h);
return $return;
}
function halt($msg) {
$this->Error = @mysql_error($this->Link_ID);
$this->Errno = @mysql_errno($this->Link_ID);
if ($this->locked) {
$this->unlock();
}
if ($this->Halt_On_Error == "no")
return;
$this->haltmsg($msg);
if ($this->Halt_On_Error != "report")
die("Session halted.");
}
function haltmsg($msg) {
printf("</td></tr></table><b>Database error:</b> %s<br>\n", $msg);
printf("<b>MySQL Error</b>: %s (%s)<br>\n",
$this->Errno,
$this->Error);-----------------
}
}
?>---------!!!非常感谢你的解释 我会好好学习的!!!------随便问一下有没有PHP学习交流QQ群???
等你用到了,就理解了
比如$this->query($query);----3.类中没有$query这属性 为什么能$this->query这样写?? $this->query()后面紧跟着(),明显是函数,怎么会是属性呢。。
3#说的对哦 是个函数 我太大意了 因为我平时很少见有函数这样写的$this->query();
var $Record = array();---1.用来做什么??
用于保存当前查询结果集中的当前记录内容
var $type = "mysql";--2.为什么要这样写??
通用数据库类可支持多种数据库,所以要制定数据库类型
$this->query($query);----3.类中没有$query这属性 为什么能$this->query这样写??
query是方法,在后面定义的
$this->halt("connect($Host, $User, \$Password) failed.");---4.hait()的功能,简单问以下可不解释?
hait方法在后面定义的,用于检查是否出现错误
@mysql_free_result($this->Query_ID);----5.请解释
释放当前查询结果所占的资源
function query($Query_String) {---6.括号里面的参数???
显然是SQL指令串
if ($this->Query_ID) {---7.为什么还要这样做呢??
根据上下文,完全没有必要这样做
function next_record() {---------------8该函数的功能???
读取下一条记录
$this->Row += 1; ------9.为什么要这样写??
个人喜好,Row 用于指示当前记录的序号
$stat = is_array($this->Record);--------------10.--
if (!$stat && $this->Auto_Free) { 请说明一下??
$this->free();------------------------
$stat > 0 表示读取记录成功,否则收回系统资源 function seek($pos = 0) {-----------------------------------------11.该函数的功能??
定位到指定记录
代码质量很差,没有兴趣解释了