PHP implementation PDO MySQL database operation class _php skills

Source: Internet
Author: User

The example of this article describes the PHP implementation PDO MySQL database operation class. Share to everyone for your reference. The specific analysis is as follows:

The Dbconfig class is responsible for configuring database access information , including: Server address, port, database instance name, user name, user password, character set, and so on.

The dbtemplate class sets up access to the database , mainly with the following actions:

1. QueryRows: Return multiple lines of records

2. Queryrow: Return to a single record

3. queryForInt: Query Word field, return integer

4. Queryforfloat: Query the Word field, return floating-point number (float)

5. queryfordouble: Query Word field, return floating-point number (double)

6. queryForObject: Query the Word field, return the object, the actual type has a database decision

7. Update: Executes an UPDATE statement. Insert/upadate/delete

The specific code is as follows:

Copy Code code as follows:
Class Dbconfig {
private static $dbms = "MySQL";
private static $host = ' 127.0.0.1 ';
private static $port = ' 3306 ';
private static $username = ';
private static $password = ';
private static $dbname = ';
private static $charset = ' Utf-8 ';
private static $DSN;

/**
*
* @return return PDO DSN configuration
*/
public static function Getdsn () {
if (!isset (self:: $DSN)) {
Self:: $dsn = self:: $dbms. ': host= '. Self:: $host. ';p ort= '.
Self:: $port. ';d bname= '. Self:: $dbname;
if (Strlen (self:: $charset) > 0) {
Self:: $dsn = self:: $dsn. '; charset= '. Self:: $charset;
}
}
Return self:: $DSN;
}

/**
* Set MySQL database server host
* @param the IP address of the $host host
*/
public static function Sethost ($host) {
if (Isset ($host) && strlen ($host) > 0)
Self:: $host = Trim ($host);
}

/**
* Set the MySQL database server port
* @param $port Port
*/
public static function Setport ($port) {
if (Isset ($port) && strlen ($port) > 0)
Self:: $port = Trim ($port);
}

/**
* Set up the MySQL database server login username
* @param $username
*/
public static function Setusername ($username) {
if (Isset ($username) && strlen ($username) > 0)
Self:: $username = $username;
}

/**
* Set the MySQL database server login password
* @param $password
*/
public static function SetPassword ($password) {
if (Isset ($password) && strlen ($password) > 0)
Self:: $password = $password;
}

/**
* Set the MySQL database server's database instance name
* @param $dbname Database instance Name
*/
public static function Setdbname ($dbname) {
if (Isset ($dbname) && strlen ($dbname) > 0)
Self:: $dbname = $dbname;
}

/**
* Set up Database encoding
* @param $charset
*/
public static function Setcharset ($charset) {
if (Isset ($charset) && strlen ($charset) > 0)
Self:: $charset = $charset;
}

}

/**
* A database Operation tool class
*
* @author zhjiun@gmail.com
*/
Class Dbtemplate {

/**
* Return multiple lines of records
* @param $sql
* @param $parameters
* @return Record Data
*/
Public Function QueryRows ($sql, $parameters = null) {
return $this->exequery ($sql, $parameters);
}

/**
* Return to a single record
* @param $sql
* @param $parameters
* @return
*/
Public Function Queryrow ($sql, $parameters = null) {
$rs = $this->exequery ($sql, $parameters);
if (count ($rs) > 0) {
return $rs [0];
} else {
return null;
}
}

/**
* Query Word field, return integer
* @param $sql
* @param $parameters
* @return
*/
Public Function queryForInt ($sql, $parameters = null) {
$rs = $this->exequery ($sql, $parameters);
if (count ($rs) > 0) {
return Intval ($rs [0][0]);
} else {
return null;
}
}

/**
* Query Word field, return floating-point number (float)
* @param $sql
* @param $parameters
* @return
*/
Public Function Queryforfloat ($sql, $parameters = null) {
$rs = $this->exequery ($sql, $parameters);
if (count ($rs) > 0) {
return Floatval ($rs [0][0]);
} else {
return null;
}
}

/**
* Query the Word field, return the floating-point number (double)
* @param $sql
* @param $parameters
* @return
*/
Public Function queryfordouble ($sql, $parameters = null) {
$rs = $this->exequery ($sql, $parameters);
if (count ($rs) > 0) {
return Doubleval ($rs [0][0]);
} else {
return null;
}
}

/**
* Query the Word field, return the object, the actual type has a database decision
* @param $sql
* @param $parameters
* @return
*/
Public Function queryForObject ($sql, $parameters = null) {
$rs = $this->exequery ($sql, $parameters);
if (count ($rs) > 0) {
return $rs [0][0];
} else {
return null;
}
}

/**
* Execute an UPDATE statement. insert/upadate/delete
* @param $sql
* @param $parameters
* @return affect the number of rows
*/
Public Function Update ($sql, $parameters = null) {
return $this->exeupdate ($sql, $parameters);
}

Private Function getconnection () {
$conn = new PDO (DBCONFIG::GETDSN (), Dbconfig::getusername (), Dbconfig::getpassword ());
$conn->setattribute (Pdo::attr_case, Pdo::case_upper);
return $conn;
}

Private Function Exequery ($sql, $parameters = null) {
$conn = $this->getconnection ();
$stmt = $conn->prepare ($sql);
$stmt->execute ($parameters);
$rs = $stmt->fetchall ();
$stmt = null;
$conn = null;
return $rs;
}

Private Function Exeupdate ($sql, $parameters = null) {
$conn = $this->getconnection ();
$stmt = $conn->prepare ($sql);
$stmt->execute ($parameters);
$affectedrows = $stmt->rowcount ();
$stmt = null;
$conn = null;
return $affectedrows;
}
}

PDO started in PHP5,PHP6 will default to use PDO, different from the previous version of the chaotic database operation, PDO unified access to the database, the programming has brought great convenience, this tool is based on PDO, Simulates the JdbcTemplate action class in the Java World Spring Framework.

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.