In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
MYSQL revised version of the operation class is what, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.
Operation class of MYSQL (new version after modification)
Class Mysqldb
{
/ / instructions for use:
/ / this class is written in accordance with the custom of ADO. People who have used ASP find it easier to connect to the database with ASP than to use PHP (this is my feeling)
/ / but PHP has to be written one by one, which is very tiring. This class has been completely encapsulated.
/ / you can specify a database table and a selected database when creating an instance of the class, such as: new MySQLDB ("table", "database")
/ / when querying data, you can use GetValue to get the corresponding value after Query, which can be either a field name or a sequence number that has begun with 0.
/ / insert a new value, first use AddNew, then use the corresponding field name or sequence number and field value of SetValue, and add it with Update
/ / use Edit to specify the conditions for editing records using SetValue, and finally add it with Update
/ / during the use of the class, sTName records the last database table name used, which can be used directly when specified. Future operations default to this table.
Of course, you can also specify a special table each time.
/ / nErr indicates whether the operation went wrong. SErr records the error code of the last error and records clearly which function caused the error.
Var $host= "localhost"; / / hostname
Var $user= "boot"; / / user name
Var $password= "oaserver"; / / user password
Var $linkid; / / connection value
Var $dbid; / / the result value of the database selection
Var $sTName; / / specifies the database table for the current operation
Var $sErr; / / error code
Var $nErr; / / indicates whether there is an error, 0 has no error, 1 has error
Var $nResult; / / query result value
Var $aFName; / / saves the array of FieldsName
Number of rows in var $nRows; / / query result
Number of columns in var $nCols; / / query results
Var $aNew; / / data added after the AddNew function, saved as an array
Var $NewEdit; / / determines whether an add operation is currently in progress. 0 means no, 1 means adding, and 2 indicates editing.
Var $sEditCon; / / specify conditions for editing records
Var $nOffset; / / record offset
Whether the var $EOF; / / tag reaches the end of the recordset
Var $sSQL; / / Last executed SQL statement
/ / Global variables to be used to execute Update
Var $sName; / / Field name
Used when var $sValue; / / field value AddNew
Used when var $sEdit; / / field value 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") / / constructor
{
$this- > Initialize ()
$this- > sTName=$TableName
$this- > linkid=mysql_connect ($host,$user,$password)
If (! $this- > linkid)
{
$this- > nErr=1
$this- > sErr= "MySqlDB: database connection error, please start the service!"
Return
}
$this- > dbid=mysql_select_db ($database)
If (! $this- > dbid)
{
$this- > nErr=1
$this- > sErr= "MySqlDB: selected database". $database. "does not exist!"
Return
}
}
Function IsEmpty ($Value)
{
If (is_string ($Value) & & empty ($Value))
Return true
Return false
}
Function Destroy () / / data cleanup processing
{
Mysql_query ("commit")
Mysql_close ()
}
Function PrintErr ()
{
If ($this- > nErr==1)
{
Echo ($this- > sErr. "
")
}
Else
{
Echo ("No errors
")
}
}
Function Execute ($SQL) / / execute the SQL statement directly
{
If (empty ($SQL))
{
$this- > nErr=1
$this- > sErr= "Execute: execution statement cannot be empty!"
Return false
}
$this- > sSQL=$SQL
If (! mysql_query ($SQL))
{
$this- > nErr=1
$this- > sErr= "Execute:SQL statement:". $SQL. "
MySql error: ".MySQL _ error ()
Return false
}
Return true
}
Function Query ($TableName= "", $SQL= "*", $Condition= "", $Order= "", $Sequenc= "") / / execute queries in the database
{
$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 statement:". $strSQL. "
MySql error: ". 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 ($itemo / in Cols / s / s +)
$this- > aFName [$I] = strtolower (mysql_field_name ($this- > nResult,$i))
}
Function MoveNext ()
{
If ($this- > EOF)
{
$this- > nErr=1
$this- > sErr= "MoveNext: moved to the end of the recordset!"
Return
}
$this- > nOffset++
If ($this- > nOffset > = $this- > nRows)
$this- > EOF=true
}
Function MoveTo ($Offset)
{
If (empty ($Offset))
{
$this- > nErr=1
$this- > sErr= "MoveTo: offset must be specified!"
Return
}
If (! $this- > nResult)
{
$this- > nErr=1
$this- > sErr= "MoveTo: please execute the query: Query" first
Return
}
$this- > nOffset=$Offset
}
/ / gets the value of the specified column of the specified row and returns the string
/ / if you do not specify Offset, you will get the value of the next row
/ / if nFields is not specified, the value of the row will be obtained and returned as an array
Function GetValue ($nFields=-1,$Offset=-1)
{
If ($this- > nResult==-1)
{
$this- > nErr=1
$this- > sErr= "GetValue: please execute the Query () function first!"
Return
}
If ($Offset >-1)
{
$this- > nOffset=$Offset
If ($this- > nOffset > = $this- > nRows)
{
$this- > nErr=1
$this- > sErr= "GetValue: the required offset is too large to be achieved!"
Return
}
}
If (! @ mysql_data_seek ($this- > nResult,$this- > nOffset))
{
$this- > nErr=1
$this- > sErr= "GetValue: request a record that does not exist!"
Return
}
$aResult=mysql_fetch_row ($this- > nResult)
If (is_int ($nFields) & & $nFields >-1)
{
If ($nFileds > $this- > nCols)
{
$this- > nErr=1
$this- > sErr= "GetValue: the requested column value is greater than the actual column value!"
Return
}
Return $aResult [$nFields]
}
If (is_string ($nFields))
{
$nFields=strtolower ($nFields)
For ($itemo / in Cols / s / s +)
{
If ($this- > aFName [$I] = $nFields)
Break
}
If ($iTunes subscription details-> nCols)
{
$this- > nErr=1
$this- > sErr= "GetValue: the requested column does not exist, please check it carefully!"
Return
}
Return $aResult [$I]
}
Return $aResult
}
Function AddNew ($TableName= "") / / flag to start adding data
{
$this- > Initialize ()
If (! empty ($TableName))
$this- > sTName=$TableName
If ($this- > NewEdit > 0)
{
$this- > nErr=1
$this- > sErr= "AddNew: you are adding or updating the database!"
Return
}
If (empty ($this- > sTName))
{
$this- > nErr=1
$this- > sErr= "AddNew: the database table you want to add is empty, you can specify it at construction time or you can specify it at 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 statement:" .strSQL. "
MySql error: ".MySQL _ error ()
Return
}
$this- > nCols=mysql_num_fields ($this- > nResult)
Unset ($this- > aFName)
$this- > aFName=array ()
For ($itemo / in Cols / s / s +)
$this- > aFName [$I] = strtolower (mysql_field_name ($this- > nResult,$i))
}
Function Edit ($Condition= "", $TableName= "") / / A pair of edits for specified database tables
{
$this- > Initialize ()
If (! empty ($TableName))
$this- > sTName=$TableName
$this- > sEditCon=$Condition
If (empty ($this- > sTName))
{
$this- > nErr=1
$this- > sErr= "Edit: please specify the database table before editing!"
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 statement:" .strSQL. "
MySql error: ".MySQL _ error ()
Return
}
$this- > nCols=mysql_num_fields ($this- > nResult)
Unset ($this- > aFName)
$this- > aFName=array ()
For ($itemo / in Cols / s / s +)
$this- > aFName [$I] = strtolower (mysql_field_name ($this- > nResult,$i))
}
Function SetValue ($Index,$Value) / / specify data, which is executed after AddNew
{
If ($this- > NewEdit==0)
{
$this- > nErr=1
$this- > sErr= "SetValue: please execute AddNew () or Edit () first!"
Return
}
If (is_int ($Index))
{
If ($Index$this- > nCols)
{
$this- > nErr=1
$this- > sErr= "SetValue: insert column values that do not exist!"
Return
}
$this- > aNew [$Index] = $Value
$tmpIn=$Index
}
Elseif (is_string ($Index))
{
$Index=strtolower ($Index)
For ($itemo / in Cols / s / s +)
{
If ($this- > aFName [$I] = $Index)
Break
}
If ($iTunes subscription details-> nCols)
{
$this- > nErr=1
$this- > sErr= "SetValue: insert column values that do not exist!"
Return
}
$this- > aNew [$I] = $Value
$tmpIn=$i
}
If (! empty ($this- > sName))
$this- > sName.= ","
$this- > sName.=$this- > aFName [$tmpIn]
/ / generate corresponding new values according to the type of the current field
If ($this- > s Valueyards = "# @!")
$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: the field name is". $this- > aFName [$tmpIn]. "the". $ftype. "type is not supported in the current version, please add data in another way!"
Return
}
If ($this- > NewEdit==2)
$this- > sName.= "=". $this- > sEdit
}
Function Update () / / stores the new value to the database
{
$strSQL= ""
If ($this- > NewEdit==0)
{
$this- > nErr=1
$this- > sErr= "Update: please execute AddNew () or Edit () first, and then add the value with SetValue ()!"
Return
}
If (empty ($this- > sValue))
{
$this- > nErr=1
$this- > sErr= "Update: data cannot be added or modified when the data is empty!"
Return
}
Switch ($this- > NewEdit)
{
Case 1: / / add
$strSQL= "insert into"
$strSQL.=$this- > sTName
$strSQL.= "(. $this- > sName.") "
$strSQL.= "values (". $this- > sValue. ")"
Break
Case 2: / / modify
$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 () failed to generate SQL statement, please check!"
Return
}
$this- > sSQL=$strSQL
If (! $this- > nResult=mysql_query ($strSQL))
{
$this- > nErr=1
$this- > sErr= "Update:SQL statement:". $strSQL. "
MySql error: ".MySQL _ error ()
Return
}
/ / echo ($this- > sSQL. "
")
/ / do the cleaning work
$this- > NewEdit=0
Unset ($this- > aNew)
Mysql_query ("commit")
}
}
? >
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 212
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.