php教程mysql教程データベース库教程连接类程序序
クラスcls_mysql{
var $querynum = 0;
var $link;
var $histories;var $dbhost;
var $dbuser;
var $dbpw;
var $dbcharset;
var $pconnect;
var $tablepre;
var $time;var $goneaway = 5;
function connect($dbhost, $dbuser, $dbpw, $dbname = '', $dbcharset = '', $pconnect = 0, $tablepre='', $time = 0) {
$this->dbhost = $dbhost;
$this->dbuser = $dbuser;
$this->dbpw = $dbpw;
$this->dbname = $dbname;
$this->dbcharset = $dbcharset;
$this->pconnect = $pconnect;
$this->tablepre = $tablepre;
$this->time = $time;if($pconnect) {
if(!$this->link = mysql_pconnect($dbhost, $dbuser, $dbpw)) {
$this->halt('mysql サーバーに接続できません');
}
} その他 {
if(!$this->link = mysql_connect($dbhost, $dbuser, $dbpw)) {
$this->halt('mysql サーバーに接続できません');
}
}if($this->version() > '4.1') {
if($dbcharset) {
mysql_query("setcharacter_set_connection=".$dbcharset.",character_set_results=".$dbcharset.",character_set_client=binary",$this->link);
}if($this->version() > '5.0.1') {
mysql_query("set sql_mode=''", $this->link);
}
}if($dbname) {
mysql_select_db($dbname, $this->link);
}}
関数 fetch_array($query, $result_type = mysql_assoc) {
return mysql_fetch_array($query, $result_type);
}関数 result_first($sql) {
$query = $this->query($sql);
return $this->result($query, 0);
}関数 fetch_first($sql) {
$query = $this->query($sql);
return $this->fetch_array($query);
}関数 fetch_all($sql, $id = '') {
$arr = array();
$query = $this->query($sql);
while($data = $this->fetch_array($query)) {
$id ? $arr[$data[$id]] = $data : $arr[] = $data;
}
$arr を返します;
}関数cache_gc() {
$this->query("有効期限timeの{$this->tablepre}SQLキャッシュから削除します");
}関数クエリ($sql, $type = '', $cachetime = false) {
$func = $type == 'unbuffered' && @function_exists('mysql_unbuffered_query') ? 'mysql_unbuffered_query' : 'mysql_query';
if(!($query = $func($sql, $this->link)) && $type != 'silent') {
$this->halt('mysql クエリ エラー', $sql);
}
$this->querynum++;
$this->histories[] = $sql;
$クエリを返す;
}関数affected_rows() {
return mysql_affected_rows($this->link);
}関数エラー() {
return (($this->link) ? mysql_error($this->link) : mysql_error());
}関数エラー番号() {
return intval(($this->link) ? mysql_errno($this->link) : mysql_errno());
}関数結果($query, $row) {
$query = @mysql_result($query, $row);
$クエリを返す;
}関数 num_rows($query) {
$query = mysql_num_rows($query);
$クエリを返す;
}関数 num_fields($query) {
return mysql_num_fields($query);
}関数 free_result($query) {
return mysql_free_result($query);
}関数 insert_id() {
return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("select last_insert_id()"), 0);
}関数 fetch_row($query) {
$query = mysql_fetch_row($query);
$クエリを返す;
}関数 fetch_fields($query) {
return mysql_fetch_field($query);
}関数バージョン() {
return mysql_get_server_info($this->link);
}関数 close() {
return mysql_close($this->link);
}関数停止($message = '', $sql = '') {
$error = mysql_error();
$errorno = mysql_errno();
if($errorno == 2006 && $this->goneaway-- > 0) {
$this->connect($this->dbhost, $this->dbuser, $this->dbpw, $this->dbname, $this->dbcharset, $this->pconnect, $ this->テーブルプレ、$this->time);
$this->クエリ($sql);
} その他 {
$s = 'error:'.$error.'
';
$s .= 'errno:'.$errorno.'
';
$s .= 'sql::'.$sql;
終了($s);
}
}
}