Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to implement php database operation class code

2025-01-20 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article will explain in detail how to implement the php database operation class code, the editor thinks it is very practical, so share it for you to do a reference, I hope you can get something after reading this article.

The basic process of database manipulation is:

1. Connect to the database server

2. Select a database

3. Execute the SQL statement

4. Process the result set

5. Print operation information

The related functions used are

Resource mysql_connect ([string server [, string username [, string password [, bool new_link [, int client_flags]) connects to the database server

Resource mysql_pconnect ([string server [, string username [, string password [, int client_flags]) connects to the database server, persistent connection

Int mysql_affected_rows ([resource link_identifier]) gets the number of rows of records affected by the most recent INSERT,UPDATE or DELETE query associated with link_identifier.

Bool mysql_close ([resource link_identifier]) returns TRUE if successful and FALSE if failed.

Int mysql_errno ([resource link_identifier]) returns the error number of the previous MySQL function, or 0 (zero) if there is no error.

String mysql_error ([resource link_identifier]) returns the error text of the previous MySQL function, or''(empty string) if there is no error. If the connection resource number is not specified, the error message is extracted from the MySQL server using the last successful connection.

Array mysql_fetch_array (resource result [, int result_type]) returns an array generated from rows fetched from the result set, or FALSE if there are no more rows.

Bool mysql_free_result (resource result) frees all memory associated with the result identifier result.

Int mysql_num_fields (resource result) returns the number of fields in the result set.

Int mysql_num_rows (resource result) returns the number of rows in the result set. This command is valid only for SELECT statements. To get the number of rows affected by the INSERT,UPDATE or DELETE query, use mysql_affected_rows ().

Resource mysql_query (string query [, resource link_identifier]) sends a query to the currently active database in the server associated with the specified connection identifier. If no link_identifier is specified, the last open connection is used. If there is no open connection, this function attempts to call the mysql_connect () function without arguments to establish a connection and use it. Query results will be cached

The code is as follows:

The copy code is as follows:

Class mysql {

Private $db_host; / / Database host

Private $db_user; / / database login name

Private $db_pwd; / / Database login password

Private $db_name; / / Database name

Private $db_charset; / / database character encoding

Private $db_pconn; / / persistent connection identification bit

Private $debug; / / debug enabled

Private $conn; / / Database connection ID

Private $msg = ""; / / Database manipulation information

/ / private $sql = ""; / / SQL statement to be executed

Public function _ _ construct ($db_host, $db_user, $db_pwd, $db_name, $db_chaeset = 'utf8', $db_pconn = false, $debug = false) {

$this- > db_host = $db_host

$this- > db_user = $db_user

$this- > db_pwd = $db_pwd

$this- > db_name = $db_name

$this- > db_charset = $db_chaeset

$this- > db_pconn = $db_pconn

$this- > result =''

$this- > debug = $debug

$this- > initConnect ()

}

Public function initConnect () {

If ($this- > db_pconn) {

$this- > conn = @ mysql_pconnect ($this- > db_host, $this- > db_user, $this- > db_pwd)

} else {

$this- > conn = @ mysql_connect ($this- > db_host, $this- > db_user, $this- > db_pwd)

}

If ($this- > conn) {

$this- > query ("SET NAMES". $this- > db_charset)

} else {

$this- > msg = "Database connection error, error number:". Mysql_errno (). Cause of error:. Mysql_error ()

}

$this- > selectDb ($this- > db_name)

}

Public function selectDb ($dbname) {

If ($dbname = "") {

$this- > db_name = $dbname

}

If (! mysql_select_db ($this- > db_name, $this- > conn)) {

$this- > msg = "Database unavailable"

}

}

Public function query ($sql, $debug = false) {

If (! $debug) {

$this- > result = @ mysql_query ($sql, $this- > conn)

} else {

}

If ($this- > result = = false) {

$this- > msg = "sql execution error, error number:". Mysql_errno (). Cause of error:. Mysql_error ()

}

/ / var_dump ($this- > result)

}

Public function select ($tableName, $columnName = "*", $where = "") {

$sql = "SELECT". $columnName. "FROM". $tableName

$sql. = $where? "WHERE". $where: null

$this- > query ($sql)

}

Public function findAll ($tableName) {

$sql = "SELECT * FROM $tableName"

$this- > query ($sql)

}

Public function insert ($tableName, $column = array ()) {

$columnName = ""

$columnValue = ""

Foreach ($column as $key = > $value) {

ColumnName. = $key. ","

$columnValue. = "'" $value. "',

}

$columnName = substr ($columnName, 0, strlen ($columnName)-1)

$columnValue = substr ($columnValue, 0, strlen ($columnValue)-1)

$sql = "INSERT INTO $tableName ($columnName) VALUES ($columnValue)"

$this- > query ($sql)

If ($this- > result) {

$this- > msg = "data inserted successfully. The newly inserted id is:". Mysql_insert_id ($this- > conn)

}

}

Public function update ($tableName, $column = array (), $where = "") {

$updateValue = ""

Foreach ($column as $key = > $value) {

UpdateValue. = $key. "=". $value. "',

}

$updateValue = substr ($updateValue, 0, strlen ($updateValue)-1)

$sql = "UPDATE $tableName SET $updateValue"

$sql. = $where? "WHERE $where": null

$this- > query ($sql)

If ($this- > result) {

$this- > msg = "data updated successfully. Number of rows affected:". Mysql_affected_rows ($this- > conn)

}

}

Public function delete ($tableName, $where = "") {

$sql = "DELETE FROM $tableName"

$sql. = $where? "WHERE $where": null

$this- > query ($sql)

If ($this- > result) {

$this- > msg = "data deleted successfully. Number of rows affected:". Mysql_affected_rows ($this- > conn)

}

}

Public function fetchArray ($result_type = MYSQL_BOTH) {

$resultArray = array ()

$I = 0

While ($result = mysql_fetch_array ($this- > result, $result_type)) {

$resultArray [$I] = $result

$iTunes +

}

Return $resultArray

}

/ / public function fetchObject () {

/ / return mysql_fetch_object ($this- > result)

/ /}

Public function printMessage () {

Return $this- > msg

}

Public function freeResult () {

@ mysql_free_result ($this- > result)

}

Public function _ destruct () {

If (! empty ($this- > result)) {

$this- > freeResult ()

}

Mysql_close ($this- > conn)

}

}

The calling code is as follows

The copy code is as follows:

Require_once 'mysql_V1.class.php'

Require_once 'commonFun.php'

$db = new mysql ('localhost',' root','', "test")

/ / select check

$db- > select ("user", "*", "username = 'system'")

$result = $db- > fetchArray (MYSQL_ASSOC)

Print_r ($result)

Dump ($db- > printMessage ())

/ / increase in insert

/ / $userInfo = array ('username'= >' system', 'password' = > md5 ("system"))

/ / $db- > insert ("user", $userInfo)

/ / dump ($db- > printMessage ())

/ / update change

/ / $userInfo = array ('password' = > md5 ("123456"))

/ / $db- > update ("user", $userInfo, "id = 2")

/ / dump ($db- > printMessage ())

/ / delete delete

/ / $db- > delete ("user", "id = 1")

/ / dump ($db- > printMessage ())

/ / findAll query all

$db- > findAll ("user")

$result = $db- > fetchArray ()

Dump ($result)

Ps, I prefer the dump function of tp, so I copy the friendly print function in the commonFun.php file. Change it to print_r () when you use it.

On "how to achieve php database operation class code" this article is shared here, I hope the above content can be of some help to you, so that you can learn more knowledge, if you think the article is good, please share it out for more people to see.

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: 0

*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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report