<?php
class MYSQL {
var $Host = "";
var $Database = "";
var $User = "";
var $Password = "";
var $Auto_Free = 1;
var $Debug = 0;
var $Halt_On_Error = "yes";
var $Seq_Table = "db_sequence";
var $Record = array();
var $Row;
var $Errno = 0;
var $Error = "";
var $type = "mysql";
var $revision = "1.2";
var $Link_ID = 0;
var $Query_ID = 0;
var $anbbs_table;
var $AddNew;
var $AddArray = array();
var $condition;
var $ArrayCach = array();
var $array_x = array();
var $sql_fields = '';
var $connect_key = null;
function MYSQL($query="") {
if (!empty($query)) {
$this->query($query);
}
}
function connect( $Database = null, $Host = null, $User = null, $Password = null)
{
if (is_resource($this->Link_ID))
{
return $this->Link_ID;
}
global $anbbs_conf;
global $an_db_var;
global $an_db_conn;
if (is_array($Database)) {
$this->Database = $Database['Database']?$Database['Database']:$Database['d'];
$this->Host = $Database['Host']?$Database['Host']:$Database['h'];
$this->User = $Database['User']?$Database['User']:$Database['u'];
$this->Password = $Database['Password']?$Database['Password']:$Database['p'];
}
else {
if ( isset($Database)) {$this->Database = $Database;} elseif ( isset($anbbs_conf['Database'])) { $this->Database = $anbbs_conf['Database'];}
if ( isset($Host)) {$this->Host = $Host;} elseif ( isset($anbbs_conf['Host'])) {$this->Host = $anbbs_conf['Host'];}
if ( isset($User)) {$this->User = $User;} elseif ( isset($anbbs_conf['User'])){ $this->User = $anbbs_conf['User'];}
if ( isset($Password)) {$this->Password = $Password;} elseif ( isset($anbbs_conf['Password'])){ $this->Password = $anbbs_conf['Password'];}
}
$var = array();
$var['Database'] = $this->Database;
$var['Host'] = $this->Host;
$var['User'] = $this->User;
$var['Password'] = $this->Password;
if (!empty($an_db_var)) {
if ($this->Debug) {
echo "CONNECT: 判断缓存中是否存在连接……/n";
}
foreach ($an_db_var as $key => $val) {
if ($val === $var) {
if (is_resource($an_db_conn[$key])) {
$this->Link_ID = $an_db_conn[$key];
$this->connect_key = $key;
if ($this->Debug) {
echo "CONNECT: 使用缓存的连接!/n";
}
}
else {
unset($an_db_conn[$key]);
unset($an_db_var[$key]);
}
}
}
}
if (!is_resource($this->Link_ID)) {
$this->Link_ID = @mysql_connect( $this->Host, $this->User, $this->Password);
if ($this->Link_ID) {
$key = count($an_db_conn);
$an_db_var[$key] = $var;
$an_db_conn[$key] = $this->Link_ID;
$this->connect_key = $key;
if ($this->Debug) {
echo "CONNECT: 新建连接,圧入缓存。/n";
}
}
else {
$this->halt("connect($this->Host, $this->User, /$Password) 失败。<BR><font color='#ff0000'>数据库连接出错!</font><BR>/n请检查:<BR>/n1、数据库系统是否启动<BR>/n2、连接参数是否正确!");
return 0;
}
}
if (!empty($this->Database)) {
if (!@mysql_select_db($this->Database, $this->Link_ID)) {
$this->halt( "<font color='#ff0000'>无法选择数据库</font><BR>/n请检查:<BR>/n1、数据库是否存在<BR>/n2、您是否有相关操作的权限!" . $this->Database);
return 0;
}
}
$this->query('SET NAMES `gbk`');
return $this->Link_ID;
}
function debug($d=null){
if (isset($d)) {
$this->Debug = $d;
}
return $this->Debug;
}
function return_array($fetch=false)
{
if ($fetch) {
if ($stat = $this->next_record()) {
return $this->Record;
}
else {
return $stat;
}
}
else {
return $this->Record;
}
}
function return_all_array()
{
if ( empty( $this->ArrayCach)){
while ( $this->next_record()){
$this->ArrayCach[] = $this->Record;
}
}
return $this->ArrayCach;
}
function array_x($filed)
{
$this->return_all_array();
if (empty($this->ArrayCach) && !is_array($this->ArrayCach)) {
return '';
}
if ( !@array_key_exists( $filed, $this->ArrayCach[0])){
return FALSE;
}
$this->array_x = array();
foreach ( $this->ArrayCach as $val){
$this->array_x[] = $val[$filed];
}
return $this->array_x;
}
function string_x( $filed)
{
$this->array_x( $filed);
return implode( ',', $this->array_x);
}
function insert_id()
{
return mysql_insert_id($this->Link_ID);
}
function set_table( $anbbs_table)
{
if ( $anbbs_table){
$this->Table = $anbbs_table;
return true;
}
$this->halt( " set_table() 表为空。");
return false;
}
function insert_rows( $array, $anbbs_table ='')
{
if( is_array( $array[0])){
foreach( $array as $val){
$this->insert_row( $val, $anbbs_table);
}
return count( $array);
}
else{
$this->insert_row( $array,$anbbs_table );
return 1;
}
}
function insert_array( $array, $anbbs_table ='', $sql_fields='')
{
$this->insert_row($array, $anbbs_table, $sql_fields);
}
function insert_row( $array, $anbbs_table='', $sql_fields='')
{
if ( !empty($anbbs_table)) {
$this->Table = $anbbs_table;
}
if ( $this->Table == '') {
$this->halt( "方法 insert_array() 数据表为空。");
}
if ( !is_array( $array)) {
$this->halt( "方法 insert_array() 第1个参数不是数组错误, 表名为 {$this->Table} ");
}
if (empty($array)) {
return 0;
}
if ($sql_fields) {
$this->set_sql_fields($sql_fields);
}
foreach( $array as $key => $val ) {
if ($this->_check_sql_fields($key)){
$values .= $val . ',';
}
else{
$values .= "'{$val}',";
}
$cols .= "`" . trim($key) . "`,";
}
$cols = substr( $cols, 0, -1);
$values = substr( $values, 0, -1);
$sql = "INSERT INTO `{$this->Table}` ({$cols}) VALUES ({$values})";
if ( $this->Debug){
echo "插入数组语句:";
}
return $this->query($sql);
}
function addnew( $anbbs_table='')
{
$this->AddNew = 1;
$this->AddArray = array();
return $this->set_table( $anbbs_table );
}
function add( $array, $val ='')
{
if ( !is_array ($array)){
if ( $val=='' ) {
return false;
}
else {
$this->AddArray[$array] = $val;
return true;
}
}
else {
foreach ( $array as $key=>$val ) {
$this->AddArray[$key] = $val;
}
return true;
}
}
function update()
{
if( $this->insert_row( $this->AddArray )){
$t = $this->AddNew;
$this->AddNew = $t + 1;
return $t;
}
else {
return false;
}
}
function update_table( $array, $condition, $anbbs_table='', $sql_fields='')
{
if ( !is_array($array)){
$this->halt(" update_table( /$array, $condition, $anbbs_table) 错误:第一个参数不是数组!");
}
if ( empty( $anbbs_table)) {
if ( empty( $this->Table)){
$this->halt(" update_table() 表为空。");
return false;
}
$anbbs_table = $this->Table;
}
if ( $condition) {
$this->condition = $condition;
}
if ($sql_fields) {
$this->set_sql_fields($sql_fields);
}
foreach ( $array as $key=>$val ) {
if ($this->_check_sql_fields($val)){
$tem .= " `{$key}`={$val},";
}
else{
$tem .= " `{$key}`='{$val}',";
}
}
$tem = substr($tem, 0, -1);
$sql = "UPDATE {$anbbs_table} SET {$tem} WHERE {$this->condition}";
if ( $this->Debug){
echo "更新数组语句:";
}
$this->query($sql);
return $this->affected_rows();
}
function insert_update( $array, $update_fild, $anbbs_table='', $sql_fields='')
{
if ( empty( $array) && !is_array($array)){
$this->halt(" insert_update( /$array, $update_fild, $anbbs_table) 错误:第1个参数不是数组!");
}
if ( empty( $update_fild) && !is_array($update_fild)){
$this->halt(" insert_update( /$array, $update_fild, $anbbs_table) 错误:第2个参数不是数组!");
}
if ( empty( $anbbs_table)){
if ( empty( $this->Table)){
return false;
}
$anbbs_table = $this->Table;
}
foreach ( $array as $key=>$val ) {
$tem .= " `{$key}`='{$val}' and";
}
$s_where = substr($tem, 0, -4);
$sql = "select count(*) from `{$anbbs_table}` WHERE {$s_where} limit 1";
$this->query($sql);
$this->next_record();
if ( $this->Debug){
echo "插入|更新数组语句-";
}
if ($this->f( 'count(*)') < 1){
return $this->insert_row( $array, $anbbs_table, $sql_fields);
}
return update_table( $update_fild, $s_where, $anbbs_table, $sql_fields);
}
function set_sql_fields($fields='')
{
$this->sql_fields = ',' . $fields . ',';
}
function _check_sql_fields($s_field)
{
if ($s_field && $this->sql_fields) {
if (strpos($this->sql_fields, ',' . $s_field . ',')) {
return true;
}
return false;
}
return 0;
}
function close($link=false)
{
$this->free();
if ($link && $this->Link_ID)
{
global $an_db_var, $an_db_conn;
@mysql_close($this->Link_ID);
unset($an_db_var[$this->connect_key]);
unset($an_db_conn[$this->connect_key]);
}
}
function link_id()
{
return $this->Link_ID;
}
function query_id()
{
return $this->Query_ID;
}
function free()
{
if ($this->Query_ID) {
@mysql_free_result($this->Query_ID);
}
$this->Query_ID = 0;
$this->Row = 0;
$this->ArrayCach = array();
$this->array_x = array();
$this->Record = array();
$this->Table = '';
$this->AddNew = array();
$this->AddArray = array();
$this->condition = '';
$this->sql_fields = '';
$this->connect_key = null;
}
function query( $Query_String, $fetch=false)
{
if (empty($Query_String)) {
return 0;
}
if (!$this->connect()) {
return 0;
}
if (empty($this->Database)) {
$this->halt( "<font color='#ff0000'>数据库为空</font><BR>/n请检查:<BR>/n1、数据库是否存在<BR>/n2、连接参数是否正确!" . $this->Database);
}
if ( $this->Query_ID) {
$this->free();
}
if ( $this->Debug) {
echo ("Debug: query = {$Query_String}<br>/n");
}
$this->Query_ID = @mysql_query( $Query_String, $this->Link_ID);
$this->Errno = mysql_errno();
$this->Error = mysql_error();
if ( !$this->Query_ID) {
//$this->halt( "错误的 SQL 语句:/n<br> " . $Query_String);
$this->halt( "错误的 SQL 语句:/n<br> ");
}
if ($fetch) {
return $this->return_all_array();
}
return $this->Query_ID;
}
function next_record( $type=MYSQL_ASSOC)
{
if ( !$this->Query_ID) {
$this->halt( "方法 next_record 调用前没有查询结果。");
return 0;
}
$this->Record = @mysql_fetch_array( $this->Query_ID, $type);
$this->Row += 1;
$this->Errno = mysql_errno();
$this->Error = mysql_error();
$stat = is_array( $this->Record);
if ( !$stat && $this->Auto_Free){
@mysql_close($this->Link_ID);
}
return $stat;
}
function seek( $pos = 0)
{
$status = @mysql_data_seek( $this->Query_ID, $pos);
if ( $status) {
$this->Row = $pos;
}
else {
@mysql_data_seek($this->Query_ID, $this->num_rows());
$this->Row = $this->num_rows;
return 0;
}
return 1;
}
function lock( $anbbs_table, $mode="write")
{
$this->connect();
$query = "lock tables ";
if (is_array($anbbs_table)) {
while (list($key, $value) = each($anbbs_table)) {
if ($key == "read" && $key != 0) {
$query .= "$value read, ";
}
else{
$query .= "$value $mode, ";
}
}
$query = substr( $query, 0, -2);
}
else{
$query .= "$anbbs_table $mode";
}
$res = @mysql_query( $query, $this->Link_ID);
if ( !$res){
$this->halt( "lock($anbbs_table, $mode) 失败。");
return 0;
}
return $res;
}
function unlock()
{
$this->connect();
$res = @mysql_query("unlock tables");
if (!$res) {
$this->halt("unlock() 失败。");
return 0;
}
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 nextid( $seq_name)
{
$this->connect();
if ( $this->lock($this->Seq_Table)){
$q = sprintf("select nextid from %s where seq_name = '%s'",$this->Seq_Table,$seq_name);
$id = @mysql_query($q, $this->Link_ID);
$res = @mysql_fetch_array($id);
if (!is_array($res)) {
$currentid = 0;
$q = sprintf("insert into %s values('%s', %s)", $this->Seq_Table,$seq_name,$currentid);
$id = @mysql_query($q, $this->Link_ID);
}
else {
$currentid = $res["nextid"];
}
$nextid = $currentid + 1;
$q = sprintf("update %s set nextid = '%s' where seq_name = '%s'",$this->Seq_Table,$nextid,$seq_name);
$id = @mysql_query($q, $this->Link_ID);
$this->unlock();
}
else {
$this->halt("锁定 " . $this->Seq_Table . " - has it been created?");
return 0;
}
return $nextid;
}
function metadata( $anbbs_table = '', $full = false)
{
$count = 0;
$id = 0;
$res = array();
if ( $anbbs_table) {
$this->connect();
$id = @mysql_list_fields( $this->Database, $anbbs_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);
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{
$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 ($anbbs_table) @mysql_free_result($id);
return $res;
}
function halt( $msg)
{
$this->Error = @mysql_error($this->Link_ID);
$this->Errno = @mysql_errno($this->Link_ID);
if ($this->Halt_On_Error == "no") {
return;
}
$this->haltmsg($msg);
if ( $this->Halt_On_Error != "report") {
die("Session halted.");
}
}
function haltmsg($msg)
{
printf("<b>Database error:</b> %s<br>/n", $msg);
printf("<b>MySQL Error</b>: %s (%s)<br>/n", $this->Errno,$this->Error);
}
function table_names()
{
$this->query("SHOW TABLES");
$i = 0;
while ($info = mysql_fetch_row( $this->Query_ID)){
$return[$i]["table_name"] = $info[0];
$return[$i]["tablespace_name"] = $this->Database;
$return[$i]["database"] = $this->Database;
$i++;
}
return $return;
}
}
function an_sql_connect_close()
{
global $an_db_var, $an_db_conn;
if(!is_array($an_db_var))
{
return 0;
}
foreach($an_db_conn as $v)
{
if (is_resource($v))
{
mysql_close($v);
}
}
$an_db_var = null;
$an_db_conn = null;
return true;
}
register_shutdown_function('an_sql_connect_close');
?>