php mysql数据库操作类

前端之家收集整理的这篇文章主要介绍了php mysql数据库操作类前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

<div class="codetitle"><a style="CURSOR: pointer" data="34548" class="copybut" id="copybut34548" onclick="doCopy('code34548')"> 代码如下:

<div class="codebody" id="code34548">
<?PHP
/
MysqL数据库DB类
@packagedb
@authoryytcpt(无影)
@version2008-03-27
@copyrigthhttp://www.d5s.cn/
/
classdb{
var$connection_id="";
var$pconnect=0;
var$shutdown_queries=array();
var$queries=array();
var$query_id="";
var$query_count=0;
var$record_row=array();
var$Failed=0;
var$halt="";
var$query_log=array();
functionconnect($db_config){
if($this->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["password"]);
}
if(!$this->connection_id){
$this->halt("CannotconnectMysqLServer");
}
if(!@MysqL_select_db($db_config["database"],$this->connection_id)){
$this->halt("CannotconnectMysqLDatabase");
}
if($db_config["charset"]){
@MysqL_unbuffered_query("SETNAMES'".$db_config["charset"]."'");
}
returntrue;
}
//发送SQL查询,并返回结果集
functionquery($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("查询失败:\n$query_id");
}
$this->query_count++;
$this->query_log[]=$str;
return$this->query_id;
}
//发送SQL查询,并不获取和缓存结果的行
functionquery_unbuffered($sql=""){
return$this->query($sql,'MysqL_unbuffered_query');
}
//从结果集中取得一行作为关联数组
functionfetch_array($sql=""){
if($sql=="")$sql=$this->query_id;
$this->record_row=@MysqL_fetch_array($sql,MysqL_ASSOC);
return$this->record_row;
}
functionshutdown_query($query_id=""){
$this->shutdown_queries[]=$query_id;
}
//取得结果集中行的数目,仅对INSERT,UPDATE或者DELETE
functionaffected_rows(){
return@MysqL_affected_rows($this->connection_id);
}
//取得结果集中行的数目,仅对SELECT语句有效
functionnum_rows($query_id=""){
if($query_id=="")$query_id=$this->query_id;
return@MysqL_num_rows($query_id);
}
//返回上一个MysqL操作中的错误信息的数字编码
functionget_errno(){
$this->errno=@MysqL_errno($this->connection_id);
return$this->errno;
}
//取得上一步INSERT操作产生的ID
functioninsert_id(){
return@MysqL_insert_id($this->connection_id);
}
//得到查询次数
functionquery_count(){
return$this->query_count;
}
//释放结果内存
functionfree_result($query_id=""){
if($query_id=="")$query_id=$this->query_id;
@MysqL_free_result($query_id);
}
//关闭MysqL连接
functionclose_db(){
if($this->connection_id)return@MysqL_close($this->connection_id);
}
//列出MysqL数据库中的表
functionget_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;
}
//从结果集中取得列信息并作为对象返回,取得所有字段
functionget_result_fields($query_id=""){
if($query_id=="")$query_id=$this->query_id;
while($field=MysqL_fetch_field($query_id)){
$fields[]=$field;
}
return$fields;
}
//错误提示
functionhalt($the_error=""){
$message=$the_error."
\r\n";
$message.=$this->get_errno()."
\r\n";
$sql="INSERTINTOdb_error(pagename,errstr,timer)VALUES('".$_SERVER["PHP_SELF"]."','".addslashes($message)."',".time().")";
@MysqL_unbuffered_query($sql);
if(DEBUG==true){
echo"<a href="https://www.jb51.cc/tag/MysqL/" target="_blank" class="keywords">MysqL</a><a href="https://www.jb51.cc/tag/shujuku/" target="_blank" class="keywords">数据库</a><a href="https://www.jb51.cc/tag/cuowu/" target="_blank" class="keywords">错误</a>";
echo"<styletype=\"text/css\">\r\n";
echo"\r\n";
echo"
\r\n";
echo"<textareaclass=\"error\"rows=\"15\"cols=\"100\"wrap=\"on\">".htmlspecialchars($message)."\r\n";
echo"
\r\n";
exit;
}
}
function__destruct(){
$this->shutdown_queries=array();
$this->close_db();
}
functionsql_select($tbname,$where="",$limit=0,$fields="
",$orderby="id",$sort="DESC"){
$sql="SELECT".$fields."FROM".$tbname."".($where?"WHERE".$where:"")."ORDERBY".$orderby."".$sort.($limit?"limit".$limit:"");
return$sql;
}
functionsql_insert($tbname,$row){
foreach($rowas$key=>$value){
$sqlfield.=$key.",";
$sqlvalue.="'".$value."',";
}
return"INSERTINTO".$tbname."(".substr($sqlfield,-1).")VALUES(".substr($sqlvalue,-1).")";
}
functionsql_update($tbname,$row,$where){
foreach($rowas$key=>$value){
$sqlud.=$key."='".$value."',";
}
return"UPDATE".$tbname."SET".substr($sqlud,-1)."WHERE".$where;
}
functionsql_delete($tbname,$where){
return"DELETEFROM".$tbname."WHERE".$where;
}
//新增加一条记录
functionrow_insert($tbname,$row){
$sql=$this->sql_insert($tbname,$row);
return$this->query_unbuffered($sql);
}
//更新指定记录
functionrow_update($tbname,$where){
$sql=$this->sql_update($tbname,$where);
return$this->query_unbuffered($sql);
}
//删除满足条件的记录
functionrow_delete($tbname,$where){
$sql=$this->sql_delete($tbname,$where);
return$this->query_unbuffered($sql);
}
/根据条件查询,返回所有记录
$tbname表名,$where查询条件,$limit返回记录,$fields返回字段
*/
functionrow_select($tbname,$sort="DESC"){
$sql=$this->sql_select($tbname,$where,$limit,$fields,$orderby,$sort);
return$this->row_query($sql);
}
//详细显示一条记录
functionrow_select_one($tbname,$orderby="id"){
$sql=$this->sql_select($tbname,1,$orderby);
return$this->row_query_one($sql);
}
functionrow_query($sql){
$rs=$this->query($sql);
$rs_num=$this->num_rows($rs);
$rows=array();
for($i=0;$i<$rs_num;$i++){
$rows[]=$this->fetch_array($rs);
}
$this->free_result($rs);
return$rows;
}
functionrow_query_one($sql){
$rs=$this->query($sql);
$row=$this->fetch_array($rs);
$this->free_result($rs);
return$row;
}
//计数统计
functionrow_count($tbname,$where=""){
$sql="SELECTcount(id)asrow_sumFROM".$tbname."".($where?"WHERE".$where:"");
$row=$this->row_query_one($sql);
return$row["row_sum"];
}
}
?>

很久没有发帖了,把我常用的一些PHP文件分享出来。
如果您加了新功能,或者是有改进,请与大家一起分享
<div class="codetitle"><a style="CURSOR: pointer" data="26262" class="copybut" id="copybut26262" onclick="doCopy('code26262')"> 代码如下:
<div class="codebody" id="code26262">
<?PHP
$db_config["hostname"]="127.0.0.1";//服务器地址
$db_config["username"]="root";//数据库用户名
$db_config["password"]="root";//数据库密码
$db_config["database"]="wap_blueidea_com";//数据库名称
$db_config["charset"]="utf8";
include('db.PHP');
$db=newdb();
$db->connect($db_config);
//例:查询表table_name中cid=1的所有记录。
$row=$db->row_select('table_name','cid=1');
?>

更详细的使用方法,请参考db类文件中的注释。

原文链接:https://www.f2er.com/php/29647.html

猜你在找的PHP相关文章