首页 数据库 mysql教程 MYSQL的操作类(已封装)

MYSQL的操作类(已封装)

Jun 07, 2016 pm 04:05 PM
class mysql 封装 操作

class MySQLDB { //MYSQL数据库操作类 //作者:熊毅 //版本:2.0(发行版) //可以自由转载,修改请通知我scxy78@yeah.net //转载请保留以上声明 //使用说明: //该类完全按照ADO的习惯书写的,用过ASP的人都觉得ASP连接数据库比PHP好用(这是我的感觉), //

class MySQLDB
{
//MYSQL数据库操作类
//作者:熊毅
//版本:2.0(发行版)
//可以自由转载,修改请通知我scxy78@yeah.net
//转载请保留以上声明
//使用说明:
//该类完全按照ADO的习惯书写的,用过ASP的人都觉得ASP连接数据库比PHP好用(这是我的感觉),
//但PHP得一个一个API地写,挺累,该类做了完全的封装
//创建类的实例时可以指定一个数据库表和选择的数据库,如:new MySQLDB("table","database");
//查询数据时Query后可以用GetValue得到相应的值,既可以是字段名也可以是已0开始的序号
//插入新值,先用AddNew后使用SetValue相应的字段名或序号和字段值,在用Update添加
//编辑时用Edit指定编辑记录的条件在使用SetValue,最后用Update添加
//在类使用过程中,sTName记录上次使用的数据库表名,当指定后可以直接使用,以后的操作默认在该表
//上进行操作,当然也可以每次指定特殊的表进行操作
//nErr指示是否操作出错,sErr记录最后一次出错的错误代码,记录了明确的有哪个函数引起的错误
//错误之处请指正
//欢迎来信与我交流编程经验:scxy78@yeah.net
//我的CSDN:用户号:scxy;呢称:小熊,请多关照
//可以自由转载,修改请通知我scxy78@yeah.net
//转载请保留以上声明

var $host="localhost"; //主机名
var $user="boot"; //用户名
var $password="oaserver"; //用户密码
var $linkid; //连接值
var $dbid; //数据库选择的结果值
var $sTName; //指定当前操作的数据库表
var $sErr; //错误代码
var $nErr; //指示是否有错误存在,0无错误,1有错误
var $nResult; //查询结果值
var $aFName; //保存FieldsName的数组
var $nRows; //查询结果中的行数
var $nCols; //查询结果中的列数
var $aNew; //添加在AddNew函数后的数据,以数组形式保存
var $NewEdit; //判断当前是否在进行添加操作,0表示没有,1表示在进行添加,2表示编辑
var $sEditCon; //指定编辑记录的条件
var $nOffset; //记录偏移量
var $EOF; //标记是否到记录集尾
var $sSQL; //最后一条执行的SQL语句

//执行Update所要用到的全局变量
var $sName; //字段名
var $sValue; //字段值AddNew时用
var $sEdit; //字段值Edit时用

function Initialize()
{
$this->nErr=0;
$this->NewEdit=0;
$this->nResult=-1;
$this->nCols=0;
$this->nRows=0;
$this->nOffset=0;
$this->EOF=true;
$this->sName="";
$this->sValue="#@!";
$this->sEdit="#@!";
unset($this->aFName);
unset($this->aNew);
}
function MySqlDB($TableName="",$database="slt") //构造函数
{
$this->Initialize();
$this->sTName=$TableName;
$this->linkid=mysql_connect($host,$user,$password);
if(!$this->linkid)
{
$this->nErr=1;
$this->sErr="MySqlDB:数据库连接出错,请启动服务!";
return;
}
$this->dbid=mysql_select_db($database);
if(!$this->dbid)
{
$this->nErr=1;
$this->sErr="MySqlDB:选择的数据库".$database."不存在!";
return;
}
}

function IsEmpty($Value)
{
if(is_string($Value)&&empty($Value))
return true;
return false;
}

function Destroy() //数据清除处理
{
mysql_query("commit");
mysql_close();
}

function PrintErr()
{
if($this->nErr==1)
{
echo($this->sErr."
");
}
else
{
echo("没有错误
");
}
}

function Execute($SQL) //直接执行SQL语句
{
if(empty($SQL))
{
$this->nErr=1;
$this->sErr="Execute:执行语句不能为空!";
return false;
}
$this->sSQL=$SQL;
if(!mysql_query($SQL))
{
$this->nErr=1;
$this->sErr="Execute:SQL语句:".$SQL."
MySql错误:".mysql_error();
return false;
}
return true;
}

function Query($TableName="",$SQL="*",$Condition="",$Order="",$Sequenc="") //在数据库里执行查询
{
$this->Initialize();
if(!empty($TableName))
$this->sTName=$TableName;
$strSQL="select ".$SQL." from ".$this->sTName;
if(!empty($Condition))
$strSQL=$strSQL." where ".$Condition;
if(!empty($Order))
$strSQL=$strSQL." order by ".$Order;
if(!empty($Sequenc))
$strSQL=$strSQL." ".$Sequenc;
$this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Query:SQL语句:".$strSQL."
MySql错误:".mysql_error()."
";
return;
}
$this->nOffset=0;
$this->nRows=mysql_num_rows($this->nResult);
$this->nCols=mysql_num_fields($this->nResult);
if($this->nRows>0)
$this->EOF=false;
else
$this->EOF=true;
unset($this->aFName);
$this->aFName=array();
for($i=0;$inCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}

function MoveNext()
{
if($this->EOF)
{
$this->nErr=1;
$this->sErr="MoveNext:已经移到记录集末尾!";
return;
}
$this->nOffset++;
if($this->nOffset>=$this->nRows)
$this->EOF=true;
}

function MoveTo($Offset)
{
if(empty($Offset))
{
$this->nErr=1;
$this->sErr="MoveTo:必须指定偏移量! ";
return;
}

if(!$this->nResult)
{
$this->nErr=1;
$this->sErr="MoveTo:请先执行查询:Query";
return;
}
$this->nOffset=$Offset;
}

//得到指定行的指定列的值,返回字符串
//如果不指定Offset将取得下一行的值
//如果不指定nFields将取得该行的值,并已数组形式返回
function GetValue($nFields=-1,$Offset=-1)
{
if($this->nResult==-1)
{
$this->nErr=1;
$this->sErr="GetValue:请先执行Query()函数!";
return;
}
if($Offset>-1)
{
$this->nOffset=$Offset;
if($this->nOffset>=$this->nRows)
{
$this->nErr=1;
$this->sErr="GetValue:所要求的偏移量太大,无法达到!";
return;
}
}
if(!@mysql_data_seek($this->nResult,$this->nOffset))
{
$this->nErr=1;
$this->sErr="GetValue:请求不存在的记录!";
return;
}
$aResult=mysql_fetch_row($this->nResult);
if(is_int($nFields)&&$nFields>-1)
{
if($nFileds>$this->nCols)
{
$this->nErr=1;
$this->sErr="GetValue:所请求的列值大于实际的列值!";
return;
}
return $aResult[$nFields];
}
if(is_string($nFields))
{
$nFields=strtolower($nFields);
for($i=0;$inCols;$i++)
{
if($this->aFName[$i]==$nFields)
break;
}
if($i==$this->nCols)
{
$this->nErr=1;
$this->sErr="GetValue:所请求的列不存在,请仔细检查!";
return;
}
return $aResult[$i];
}
return $aResult;
}

function AddNew($TableName="") //标志开始添加数据
{
$this->Initialize();
if(!empty($TableName))
$this->sTName=$TableName;
if($this->NewEdit>0)
{
$this->nErr=1;
$this->sErr="AddNew:你正在对数据库进行添加或更新操作!";
return;
}
if(empty($this->sTName))
{
$this->nErr=1;
$this->sErr="AddNew:想要添加的数据库表为空,可以在构造时指定,也可在AddNew()时指定!";
return;
}
unset($this->aNew);
$this->aNew=array();
$this->NewEdit=1;
$strSQL="select * from ".$this->sTName;
$this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="AddNew:SQL语句:".strSQL."
MySql错误:".mysql_error();
return;
}
$this->nCols=mysql_num_fields($this->nResult);
unset($this->aFName);
$this->aFName=array();
for($i=0;$inCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}

function Edit($Condition="",$TableName="") //对指定数据库表进行编辑
{
$this->Initialize();
if(!empty($TableName))
$this->sTName=$TableName;
$this->sEditCon=$Condition;
if(empty($this->sTName))
{
$this->nErr=1;
$this->sErr="Edit:在编辑前请先指定数据库表!";
return;
}
unset($this->aNew);
$this->aNew=array();
$this->NewEdit=2;
$strSQL="select * from ".$this->sTName;
$this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Edit:SQL语句:".strSQL."
MySql错误:".mysql_error();
return;
}
$this->nCols=mysql_num_fields($this->nResult);
unset($this->aFName);
$this->aFName=array();
for($i=0;$inCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}

function SetValue($Index,$Value) //指定数据,跟在AddNew后执行;
{
if($this->NewEdit==0)
{
$this->nErr=1;
$this->sErr="SetValue:请先执行AddNew()或者Edit()!";
return;
}
if(is_int($Index))
{
if($Index$this->nCols)
{
$this->nErr=1;
$this->sErr="SetValue:插入不存在的列值!";
return;
}
$this->aNew[$Index]=$Value;
$tmpIn=$Index;
}
elseif(is_string($Index))
{
$Index=strtolower($Index);
for($i=0;$inCols;$i++)
{
if($this->aFName[$i]==$Index)
break;
}
if($i==$this->nCols)
{
$this->nErr=1;
$this->sErr="SetValue:插入不存在的列值!";
return;
}
$this->aNew[$i]=$Value;
$tmpIn=$i;
}
if(!empty($this->sName))
$this->sName.=",";
$this->sName.=$this->aFName[$tmpIn];
//根据当前字段的类型生成相应的新值
if($this->sValue!="#@!")
$this->sValue.=",";
else
$this->sValue="";
$ftype=@mysql_field_type($this->nResult,$i);
//echo($ftype.",".$this->aNew[$i].",".$i.":".$sValue."
");
switch($ftype)
{
case "string":
case "date":
case "datetime":
$this->sValue.="\"".$this->aNew[$tmpIn]."\"";
$this->sEdit="\"".$this->aNew[$tmpIn]."\"";
break;
case "int":
case "unknown":
$this->sValue.=$this->aNew[$tmpIn];
$this->sEdit=$this->aNew[$tmpIn];
break;
default:
$this->nErr=1;
$this->sErr="Update:字段名为".$this->aFName[$tmpIn]."的".$ftype."类型目前版本不支持,请用别的方法添加数据!";
return;
}

if($this->NewEdit==2)
$this->sName.="=".$this->sEdit;
}

function Update() //存储新值到数据库
{
$strSQL="";

if($this->NewEdit==0)
{
$this->nErr=1;
$this->sErr="Update:请先执行AddNew()或者Edit(),再用SetValue()添加值!";
return;
}

if(empty($this->sValue))
{
$this->nErr=1;
$this->sErr="Update:在数据为空的情况下,不能添加或修改数据!";
return;
}

switch($this->NewEdit)
{
case 1: //添加
$strSQL="insert into ";
$strSQL.=$this->sTName;
$strSQL.=" (".$this->sName.") ";
$strSQL.="values (".$this->sValue.")";
break;
case 2: //修改
$strSQL="update ";
$strSQL.=$this->sTName;
$strSQL.=" set ";
$strSQL.=$this->sName;
if(!empty($this->sEditCon))
$strSQL.=" where ".$this->sEditCon;
break;
default:
$this->nErr=1;
$this->sErr="Update:Update()生成SQL语句出错,请检查!";
return;
}

$this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Update:SQL语句:".$strSQL."
MySql错误:".mysql_error();
return;
}
//echo($this->sSQL."
");
//作清理工作
$this->NewEdit=0;
unset($this->aNew);
mysql_query("commit");
}
}

(责任编辑:铭铭 mingming_ky@126.com TEL:(010)68476636)


本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
2 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
4 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

PHP 的大数据结构处理技巧 PHP 的大数据结构处理技巧 May 08, 2024 am 10:24 AM

大数据结构处理技巧:分块:分解数据集并分块处理,减少内存消耗。生成器:逐个产生数据项,无需加载整个数据集,适用于无限数据集。流:逐行读取文件或查询结果,适用于大文件或远程数据。外部存储:对于超大数据集,将数据存储在数据库或NoSQL中。

如何优化 PHP 中的 MySQL 查询性能? 如何优化 PHP 中的 MySQL 查询性能? Jun 03, 2024 pm 08:11 PM

可以通过以下方式优化MySQL查询性能:建立索引,将查找时间从线性复杂度降至对数复杂度。使用PreparedStatements,防止SQL注入并提高查询性能。限制查询结果,减少服务器处理的数据量。优化连接查询,包括使用适当的连接类型、创建索引和考虑使用子查询。分析查询,识别瓶颈;使用缓存,减少数据库负载;优化PHP代码,尽量减少开销。

如何在 PHP 中使用 MySQL 备份和还原? 如何在 PHP 中使用 MySQL 备份和还原? Jun 03, 2024 pm 12:19 PM

在PHP中备份和还原MySQL数据库可通过以下步骤实现:备份数据库:使用mysqldump命令转储数据库为SQL文件。还原数据库:使用mysql命令从SQL文件还原数据库。

如何使用 PHP 插入数据到 MySQL 表中? 如何使用 PHP 插入数据到 MySQL 表中? Jun 02, 2024 pm 02:26 PM

如何将数据插入MySQL表中?连接到数据库:使用mysqli建立与数据库的连接。准备SQL查询:编写一个INSERT语句以指定要插入的列和值。执行查询:使用query()方法执行插入查询,如果成功,将输出一条确认消息。

如何修复 MySQL 8.4 上的 mysql_native_password 未加载错误 如何修复 MySQL 8.4 上的 mysql_native_password 未加载错误 Dec 09, 2024 am 11:42 AM

MySQL 8.4(截至 2024 年的最新 LTS 版本)中引入的主要变化之一是默认情况下不再启用“MySQL 本机密码”插件。此外,MySQL 9.0完全删除了这个插件。 此更改会影响 PHP 和其他应用程序

如何在 PHP 中使用 MySQL 存储过程? 如何在 PHP 中使用 MySQL 存储过程? Jun 02, 2024 pm 02:13 PM

要在PHP中使用MySQL存储过程:使用PDO或MySQLi扩展连接到MySQL数据库。准备调用存储过程的语句。执行存储过程。处理结果集(如果存储过程返回结果)。关闭数据库连接。

如何使用 PHP 创建 MySQL 表? 如何使用 PHP 创建 MySQL 表? Jun 04, 2024 pm 01:57 PM

使用PHP创建MySQL表需要以下步骤:连接到数据库。创建数据库(如果不存在)。选择数据库。创建表。执行查询。关闭连接。

AMD 'Strix Halo” FP11 封装尺寸曝光:和英特尔 LGA1700 相当,比 Phoenix 大 60% AMD 'Strix Halo” FP11 封装尺寸曝光:和英特尔 LGA1700 相当,比 Phoenix 大 60% Jul 18, 2024 am 02:04 AM

本站7月9日消息,AMDZen5架构“Strix”系列处理器会有两种封装方案,其中较小的StrixPoint将采用FP8封装,而StrixHalo将会采用FP11封装。图源:videocardz消息源@Olrak29_最新曝料称StrixHalo的FP11封装尺寸为37.5mm*45mm(1687平方毫米),和英特尔AlderLake、RaptorLakeCPU的LGA-1700封装尺寸相同。AMD最新的PhoenixAPU采用FP8封装方案,尺寸为25*40mm,这意味着StrixHalo的F

See all articles