class DB_HOST
{
public $host = 'localhost';
public  $user = 'eikioa';
public  $password = 'eikioa';
public $port = 3306;
}

解决方案 »

  1.   

    class DB_MYSQL extends DB_HOST
    {
    //============================================================
    public $database = 'eikioa';
    //============================================================
    public  $link_id = 0;
    public  $query_id = 0;
    private $row_result = array();
    private $field_result = array();
    private $affected_rows;
    private $rows;
    private $fields;
    private $row_position = 0;

    public  $insert_id = 0;
    public  $sql;
    public  $debug;

    //备份
    //public  $backup_cmd = '/usr/bin/mysqldump';
    //************************************************************

    function __construct()
    {}

    function __destruct()
    {} function connect($database = '',$host = '',$user = '',$password = '')
    {
    $database = $database == '' ? $this->database : $database;
    $host = $host == '' ? $this->host : $host;
    $user = $user == '' ? $this->user : $user;
    $password = $password == '' ? $this->password : $password;
    //-----------------------------------------------------------//
    if(0 == $this->link_id)
    {
    $this->link_id = mysql_pconnect($host,$user,$password);
    if(!$this->link_id)
    {
    $this->halt("can't connect to host".$this->host);
    }
    if(!mysql_select_db($this->database,$this->link_id))
    {
    $this->halt("cant't select the database : ".$this->database);
    }
    }
    }

    function select_db($db)
    {
    $this->database = $db;
    }

    function free()
    {
    if(@mysql_free_result($this->query_id))
    {
    unset($this->row_result);
    }
    $this->query_id = 0;
    }

    function query($query_string)
    {
    $this->sql = $query_string;

    if($this->query_id)
    {
    $this->free();
    }
    if(0 == $this->link_id)
    {
    $this->connect();
    }

    $ct_start = $this->debug == true ? mktime(date('h'),date('i'),date('s'),date('m'),date('d'),date('Y')) : '';

    mysql_query('set names utf8');
    $this->query_id = mysql_query($query_string,$this->link_id);

    $ct_end = $this->debug == true ? mktime(date('h'),date('i'),date('s'),date('m'),date('d'),date('Y')) : '';
    echo ($this->debug == true ? '<br />SQL : '.$this->sql.' . Executed : '.($ct_end-$ct_start).' ms. <br />' : '');

    $this->insert_id = mysql_insert_id();
    if(!$this->query_id)
    {
    $this->halt($query_string);
    }
    return $this->query_id;
    }

    function seek($pos)
    {
    if(mysql_data_seek($this->query_id,$pos))
    {
    $this->row_position = $pos;
    return true;
    }
    else
    {
    $this->halt('error at function : seek().');
    return false;
    }
    }

    function get_rows_array()
    {
    $this->get_rows();
    for($i = 0; $i < $this->rows; $i++)
    {
    if(!mysql_data_seek($this->query_id,$i))
    {
    $this->halt('error at function : mysql_data_seek().');
    }
    $this->row_result[$i] = mysql_fetch_array($this->query_id);
    }
    return $this->row_result;
    }

    function get_fields_array()
    {
    $this->get_fields();
    for($i = 0; $i < $this->fields; $i++)
    {
    $obj = mysql_fetch_field($this->query_id,$i);
    $this->field_result[$i] = $obj->name;
    }
    return $this->field_result;
    }

    function get_affected_rows()
    {
    $this->affected_rows = mysql_affected_rows($this->link_id);
    return $this->affected_rows;
    }

    function get_rows()
    {
    $this->rows = mysql_num_rows($this->query_id);
    return $this->rows;
    }

    function get_fields()
    {
    $this->fields = mysql_num_fields($this->query_id);
    return $this->fields;
    }

    function fetch_one_array($sql)
    {  
    $this->query($sql);
    return mysql_fetch_array($this->query_id);
    }

    function halt($msg)
    {
    $this->error = mysql_error();
    echo " <html>
    <head>
    <meta http-equiv='content-type' content='text/html; charset=utf-8'>
    </head>
    <body>
    <font style='font-family:arial;font-size:12px;'><br />
    <br /><b>SQL : </b>".$this->sql." <br />
    <br /><b>mysql return error : </b> $this->error <br />
    <br /><b>Note : </b> <br /> ".$msg." <br />
    <br /><b>URL : </b> <br /><font style='color:#0000ee;text-decoration:underline'> ".$_SERVER['PHP_SELF'] ."
    </font><br />
    <br /><b>Message : </b><br />Please sumit the error report to web master or administrator!<br />
    <br /><b><font color=red>scripit is stoped.</font></b></font>
    </body>
    </html>";
    exit();
    }

    function tables($t)
    {
    if($t == '')
    {
    $this->halt('table name is null!');
    return null;
    }
    else
    return new DB_TABLE($t);
    }
    }
      

  2.   

    class DB_TABLE extends DB_MYSQL
    {
    public $table_name; //表名

    function __construct($t)
    {
    $this->table_name = $t;
    }

    //在表中插入数据
    function insert($arr)
    {
    if(!is_array($arr[1]))
    {
    $this->halt('Insert: upmsg is null!');
    }
    else
    {
    $u = $this->key_value_sql($arr[1],',');
    }
    $s = 'insert into '.$this->name.' set '.$u;
    $this->query($s);
    }

    //从表中删除数据
    function delete($id)
    {
    $this->query('delete from '.$this->table_name.' where id = '.$id);
    }

    //修改表中的数据
    function update($arr)
    {
    /*$arr = array(
    "upmsg" => array("fields1" => "2","fields2" => "3"),
    "ck" => array("id"=>1), //目前只支持 and 形式
    );
    */
    if(!is_array($arr[0]))
    {
    $this->halt('Update: upmsg is null!');
    }
    else
    {
    $u = $this->key_value_sql($arr[0],',');
    }

    if(!is_array($arr[1]))
    {
    $this->halt('Update: where is null!');
    }
    else
    {
    $k = $this->key_value_sql($arr[1],' and ');
    }

    $s = 'update '.$this->table_name.' set '.$u.' where '.$k;
    $this->query($s);
    }

    //依据id修改记录
    function update_by($arr,$cid)
    {
    $this->update($arr,array('id'=>$cid));
    }

    //根据条件组合sql语句
    function key_value_sql($arr,$join)
    {
    $v = array_values($arr);
    $k = array_keys($arr);
    $a = array();

    $cot = count($v);

    for($i = 0; $i < $cot; $i++)
    {
    $a[$i] = $k[$i] . '=' . $v[$i];
    }

    return implode($join,$a);
    }

    //得到部分字段的值
    function get_rows_all($part='*',$where='')
    {
    if($where != '')
    {
    $k = $this->key_value_sql($where,' and ');
    }
    $sqlstr = 'select '.$part.' from '.$this->table_name.($where == '' ? '' : ' where '.$k);
    $this->query($sqlstr);
    return $this->get_rows_array();
    }

    //依据id得到单条记录
    function find_by_id($cid,$part='*')
    {
    return $this->fetch_one_array('select '.$part.' from '.$this->table_name.' where id = '.$cid);
    }

    //得到字段集
    function get_fields_all()
    {
    $this->query('select * from '.$this->table_name.' where 1 = 2');
    return $this->get_fields_array();
    }

    //得到部分记录
    function rows($cid=0)
    {
    $this->get_rows_array();
    if($this->row_result)
    return $this->row_result[$cid];
    else
    return 0;
    }

    //得到部分字段
    function fields($fid=0)
    {
    $this->get_fields_array();
    if($this->field_result)
    return $this->field_result[$fid];
    else
    return 0;
    }
    }