操作MYSQL的一个PHP类

来源:互联网 发布:手机号注册查询软件 编辑:程序博客网 时间:2024/05/18 00:46
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>
<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

 

<?PHP
/*
* 这个类本来是PHPLIB里的一部分,我做了些修改。
*/
class MyDB_Sql {
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";

var $Link_ID = 0;
var $Query_ID = 0;
var $Record = array();
var $Row;

var $Errno = 0;
var $Error = "";

var $Auto_free = 0; ## Set this to 1 for automatic MYSQL_free_result()
var $Auto_commit = 0; ## set this to 1 to automatically commit results

var $debugmode = 0;

function connect() {
if ( 0 == $this->Link_ID ) {
$this->Link_ID=MYSQL_pconnect($this->Host, $this->User, $this->Password);
if (!$this->Link_ID) {
$this->halt("Link-ID == false, pconnect failed");
}
if (!MYSQL_query(sprintf("use %s",$this->Database),$this->Link_ID)) {
$this->halt("cannot use database ".$this->Database);
}
}
}

function query($Query_String) {
$this->connect();

if ($this->debugmode)
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() {
$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) {
MYSQL_free_result($this->Query_ID);
$this->Query_ID = 0;
}
return $stat;
}

function seek($pos) {
$status = MYSQL_data_seek($this->Query_ID, $pos);
if ($status)
$this->Row = $pos;
return;
}

function metadata($table) {
$count = 0;
$id = 0;
$res = array();

$this->connect();
$id = @MYSQL_list_fields($this->Database, $table);
if ($id < 0) {
$this->Errno = MYSQL_errno();
$this->Error = MYSQL_error();
$this->halt("Metadata query failed.");
}
$count = MYSQL_num_fields($id);

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;
$res["num_fields"]= $count;
}

MYSQL_free_result($id);
return $res;
}

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) {
return $this->Record[$Name];
}

function p($Name) {
print $this->Record[$Name];
}

function halt($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);
die("Session halted.");
}
}

class SQL extends MyDB_Sql {
var $Host = "localhost";
var $Database = "";
var $User = "";
var $Password = "";

function free_result() {
return @MYSQL_free_result($this->Query_ID);
}

function rollback() {
return 1;
}

function commit() {
return 1;
}

function autocommit($onezero) {
return 1;
}

function insert_id($col="",$tbl="",$qual="") {
return MYSQL_insert_id($this->Query_ID);
}
}
?>

 <script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 728x15, 创建于 08-4-23MSDN */google_ad_slot = "3624277373";google_ad_width = 728;google_ad_height = 15;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>

<script type="text/javascript"><!--google_ad_client = "pub-2947489232296736";/* 160x600, 创建于 08-4-23MSDN */google_ad_slot = "4367022601";google_ad_width = 160;google_ad_height = 600;//--></script><script type="text/javascript"src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>