search
HomeDatabaseMysql Tutorialmysql仿asp的数据库操作类

mysql仿asp的数据库操作类

CODE:[复制到剪切板]class MySQLDB
{
//MYSQL数据库操作类
//作者:熊毅
//版本:2.0(发行版)

//可以自由转载,修改请通知我scxy78@yeah.net
//转载请保留以上声明

//上进行操作,当然也可以每次指定特殊的表进行操作
//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");
}
}
?>
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Explain the InnoDB Buffer Pool and its importance for performance.Explain the InnoDB Buffer Pool and its importance for performance.Apr 19, 2025 am 12:24 AM

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

MySQL vs. Other Programming Languages: A ComparisonMySQL vs. Other Programming Languages: A ComparisonApr 19, 2025 am 12:22 AM

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages ​​such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages ​​have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

Learning MySQL: A Step-by-Step Guide for New UsersLearning MySQL: A Step-by-Step Guide for New UsersApr 19, 2025 am 12:19 AM

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL: Essential Skills for Beginners to MasterMySQL: Essential Skills for Beginners to MasterApr 18, 2025 am 12:24 AM

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL: Structured Data and Relational DatabasesMySQL: Structured Data and Relational DatabasesApr 18, 2025 am 12:22 AM

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL: Key Features and Capabilities ExplainedMySQL: Key Features and Capabilities ExplainedApr 18, 2025 am 12:17 AM

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

The Purpose of SQL: Interacting with MySQL DatabasesThe Purpose of SQL: Interacting with MySQL DatabasesApr 18, 2025 am 12:12 AM

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

MySQL for Beginners: Getting Started with Database ManagementMySQL for Beginners: Getting Started with Database ManagementApr 18, 2025 am 12:10 AM

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft