仓酷云

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 579|回复: 7
打印 上一主题 下一主题

[学习教程] MYSQL网页设计MYSQL的操纵类(修正后的新版本)

[复制链接]
不帅 该用户已被删除
跳转到指定楼层
楼主
发表于 2015-1-16 22:45:20 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
如果你在一个遵循GPL的自由(开源)项目中使用MySQL,那么你可以遵循GPL协议使用MySQL。然而,如果你的项目不是在GPL协议下的话,你必须为使用MySQL来支付许可费用,或者你可能因为这个因素而将你的项目改为遵循GPL。mysql









classMySQLDB
{
//MYSQL数据库操纵类
//熊毅
//版本:2.0(刊行版)

//能够自在转载,修正请关照我scxy78@yeah.net
//转载请保存以上声明

//利用申明:
//该类完整依照ADO的习气誊写的,用过ASP的人都以为ASP毗连数据库比PHP好用(这是我的感到),
//但PHP得一个一个API地写,挺累,该类做了完整的封装
//创立类的实例时能够指定一个数据库表和选择的数据库,如:newMySQLDB("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时用

functionInitialize()
{
$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);
}
functionMySqlDB($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;
}
}

functionIsEmpty($Value)
{
if(is_string($Value)&&empty($Value))
returntrue;
returnfalse;
}

functionDestroy()//数据扫除处置
{
mysql_query("commit");
mysql_close();
}

functionPrintErr()
{
if($this->nErr==1)
{
echo($this->sErr."<br><br>");
}
else
{
echo("没有毛病<br><br>");
}
}

functionExecute($SQL)//间接实行SQL语句
{
if(empty($SQL))
{
$this->nErr=1;
$this->sErr="Execute:实行语句不克不及为空!";
returnfalse;
}
$this->sSQL=$SQL;
if(!mysql_query($SQL))
{
$this->nErr=1;
$this->sErr="Execute:SQL语句:".$SQL."<br>MySql毛病:".mysql_error();
returnfalse;
}
returntrue;
}

functionQuery($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."orderby".$Order;
if(!empty($Sequenc))
$strSQL=$strSQL."".$Sequenc;
$this->sSQL=$strSQL;
if(!$this->nResult=mysql_query($strSQL))
{
$this->nErr=1;
$this->sErr="Query:SQL语句:".$strSQL."<br>MySql毛病:".mysql_error()."<br>";
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;$i<$this->nCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}

functionMoveNext()
{
if($this->EOF)
{
$this->nErr=1;
$this->sErr="MoveNext:已移到纪录集开端!";
return;
}
$this->nOffset++;
if($this->nOffset>=$this->nRows)
$this->EOF=true;
}

functionMoveTo($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将获得该行的值,并已数组情势前往
functionGetValue($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;$i<$this->nCols;$i++)
{
if($this->aFName[$i]==$nFields)
break;
}
if($i==$this->nCols)
{
$this->nErr=1;
$this->sErr="GetValue:所哀求的列不存在,请细心反省!";
return;
}
return$aResult[$i];
}
return$aResult;
}

functionAddNew($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."<br><br>MySql毛病:".mysql_error();
return;
}
$this->nCols=mysql_num_fields($this->nResult);
unset($this->aFName);
$this->aFName=array();
for($i=0;$i<$this->nCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}

functionEdit($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."<br><br>MySql毛病:".mysql_error();
return;
}
$this->nCols=mysql_num_fields($this->nResult);
unset($this->aFName);
$this->aFName=array();
for($i=0;$i<$this->nCols;$i++)
$this->aFName[$i]=strtolower(mysql_field_name($this->nResult,$i));
}

functionSetValue($Index,$Value)//指定命据,跟在AddNew后实行;
{
if($this->NewEdit==0)
{
$this->nErr=1;
$this->sErr="SetValue:请先实行AddNew()大概Edit()!";
return;
}
if(is_int($Index))
{
if($Index<0||$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;$i<$this->nCols;$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."<br>");

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;
}

functionUpdate()//存储新值到数据库
{
$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)
{
case1://增加
$strSQL="insertinto";
$strSQL.=$this->sTName;
$strSQL.="(".$this->sName.")";
$strSQL.="values(".$this->sValue.")";
break;
case2://修正
$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."<br><br>MySql毛病:".mysql_error();
return;
}
//echo($this->sSQL."<br>");
//作清算事情
$this->NewEdit=0;
unset($this->aNew);
mysql_query("commit");
}
}
?>
DBaaS系统本身并不提供面对面访问或个人客户关系或持续不断的支持MySQL学习教程。这些就是需要解决方案提供商的原因。他们帮助客户选择正确的解决方案、规划集成和迁移战略,然后协助实施。
小妖女 该用户已被删除
沙发
发表于 2015-1-19 22:42:10 | 只看该作者
两个月啃那本sqlserver2005技术内部-存储引擎,花了几个月啃四本书
山那边是海 该用户已被删除
板凳
发表于 2015-2-4 23:54:06 | 只看该作者
如果你是从“学习某一种数据库应用软件,从而获得应聘的资本和工作机会”的角度来问的话。
柔情似水 该用户已被删除
地板
发表于 2015-2-10 23:24:49 | 只看该作者
财务软件要用SQL也只是后台的数据库而已,软件都是成品的,当然多学东西肯定是有好处的..
admin 该用户已被删除
5#
发表于 2015-3-1 17:26:54 | 只看该作者
所以你总能得到相应的升级版本,来满足你的需求。
兰色精灵 该用户已被删除
6#
发表于 2015-3-10 21:30:58 | 只看该作者
多加的系统视图和实时系统信息这些东西对DBA挑优非常有帮助,但是感觉粒度还是不太细。
冷月葬花魂 该用户已被删除
7#
发表于 2015-3-17 10:28:04 | 只看该作者
这一点很好的加强了profiler的功能。但是提到profiler提醒大家注意一点。windows2003要安装sp1补丁才能启动profiler。否则点击没有反应。
莫相离 该用户已被删除
8#
发表于 2015-3-24 07:24:11 | 只看该作者
大侠们有推荐的书籍和学习方法写下吧。
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|仓酷云 鄂ICP备14007578号-2

GMT+8, 2024-9-28 04:06

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表