php mysqlデータベース操作クラス、強力な機能

6715 ワード

pconnect){
$this->connection_id = mysql_pconnect($db_config["hostname"], $db_config["username"], $db_config["password"]);
}else{
$this->connection_id = mysql_connect($db_config["hostname"], $db_config["username"], $db_config["password"]);
}
if ( ! $this->connection_id ){
$this->halt("Can not connect MySQL Server");
}
if ( ! @mysql_select_db($db_config["database"], $this->connection_id) ){
$this->halt("Can not connect MySQL Database");
}
if ($db_config["charset"]) {
@mysql_unbuffered_query("SET NAMES '".$db_config["charset"]."'");
}
return true;
}
//  SQL   ,      
function query($query_id, $query_type='mysql_query'){
$this->query_id = $query_type($query_id, $this->connection_id);
$this->queries[] = $query_id;
if (! $this->query_id ) {
$this->halt("    :
$query_id"); } $this->query_count++; $this->query_log[] = $str; return $this->query_id; } // SQL , function query_unbuffered($sql=""){ return $this->query($sql, 'mysql_unbuffered_query'); } // function fetch_array($sql = ""){ if ($sql == "") $sql = $this->query_id; $this->record_row = @mysql_fetch_array($sql, MYSQL_ASSOC); return $this->record_row; } function shutdown_query($query_id = ""){ $this->shutdown_queries[] = $query_id; } // , INSERT,UPDATE DELETE function affected_rows() { return @mysql_affected_rows($this->connection_id); } // , SELECT function num_rows($query_id="") { if ($query_id == "") $query_id = $this->query_id; return @mysql_num_rows($query_id); } // MySQL function get_errno(){ $this->errno = @mysql_errno($this->connection_id); return $this->errno; } // INSERT ID function insert_id(){ return @mysql_insert_id($this->connection_id); } // function query_count() { return $this->query_count; } // function free_result($query_id=""){ if ($query_id == "") $query_id = $this->query_id; @mysql_free_result($query_id); } // MySQL function close_db(){ if ( $this->connection_id ) return @mysql_close( $this->connection_id ); } // MySQL function get_table_names(){ global $db_config; $result = mysql_list_tables($db_config["database"]); $num_tables = @mysql_numrows($result); for ($i = 0; $i < $num_tables; $i++) { $tables[] = mysql_tablename($result, $i); } mysql_free_result($result); return $tables; } // , function get_result_fields($query_id=""){ if ($query_id == "") $query_id = $this->query_id; while ($field = mysql_fetch_field($query_id)) { $fields[] = $field; } return $fields; } // function halt($the_error=""){ $message = $the_error."
\r
"; $message.= $this->get_errno() . "
\r
"; $sql = "INSERT INTO `db_error`(pagename, errstr, timer) VALUES('".$_SERVER["PHP_SELF"]."', '".addslashes($message)."', ".time().")"; @mysql_unbuffered_query($sql); if (DEBUG==true){ echo "MySQL "; echo "\r
"; echo "\r
"; echo "
\r
"; echo "\r
"; echo "
\r
"; exit; } } function __destruct(){ $this->shutdown_queries = array(); $this->close_db(); } function sql_select($tbname, $where="", $limit=0, $fields="*", $orderby="id", $sort="DESC"){ $sql = "SELECT ".$fields." FROM `".$tbname."` ".($where?" WHERE ".$where:"")." ORDER BY ".$orderby." ".$sort.($limit ? " limit ".$limit:""); return $sql; } function sql_insert($tbname, $row){ foreach ($row as $key=>$value) { $sqlfield .= $key.","; $sqlvalue .= "'".$value."',"; } return "INSERT INTO `".$tbname."`(".substr($sqlfield, 0, -1).") VALUES (".substr($sqlvalue, 0, -1).")"; } function sql_update($tbname, $row, $where){ foreach ($row as $key=>$value) { $sqlud .= $key."= '".$value."',"; } return "UPDATE `".$tbname."` SET ".substr($sqlud, 0, -1)." WHERE ".$where; } function sql_delete($tbname, $where){ return "DELETE FROM `".$tbname."` WHERE ".$where; } // function row_insert($tbname, $row){ $sql = $this->sql_insert($tbname, $row); return $this->query_unbuffered($sql); } // function row_update($tbname, $row, $where){ $sql = $this->sql_update($tbname, $row, $where); return $this->query_unbuffered($sql); } // function row_delete($tbname, $where){ $sql = $this->sql_delete($tbname, $where); return $this->query_unbuffered($sql); } /* , * $tbname , $where , $limit , $fields */ function row_select($tbname, $where="", $limit=0, $fields="*", $orderby="id", $sort="DESC"){ $sql = $this->sql_select($tbname, $where, $limit, $fields, $orderby, $sort); return $this->row_query($sql); } // function row_select_one($tbname, $where, $fields="*", $orderby="id"){ $sql = $this->sql_select($tbname, $where, 1, $fields, $orderby); return $this->row_query_one($sql); } function row_query($sql){ $rs = $this->query($sql); $rs_num = $this->num_rows($rs); $rows = array(); for($i=0; $ifetch_array($rs); } $this->free_result($rs); return $rows; } function row_query_one($sql){ $rs = $this->query($sql); $row = $this->fetch_array($rs); $this->free_result($rs); return $row; } // function row_count($tbname, $where=""){ $sql = "SELECT count(id) as row_sum FROM `".$tbname."` ".($where?" WHERE ".$where:""); $row = $this->row_query_one($sql); return $row["row_sum"]; } } ?>