PDO MySQL Database operation class _php tutorial

Source: Internet
Author: User
Tags mysql tutorial
PDO's MySQL Tutorial database tutorial Operation class
* The Dbconfig class is responsible for configuring database access information, including: Server address, port, DB instance name, user name, user password, character set, and so on.
* The Dbtemplate class sets up access operations to the database, mainly with the following operations:
1. QueryRows: Returns multiple rows of records
2. Queryrow: Return to a single record
3. queryForInt: Query single field, return integer
4. Queryforfloat: Query single field, return floating point number (float)
5. Queryfordouble: Query single field, return floating-point number (double)
6. queryForObject: Query single field, return object, actual type has database decision
7. Update: Executes an UPDATE statement. Insert/upadate/delete

*/

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 up 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 port of the MySQL database server
* @param $port Port
*/
public static function Setport ($port) {
if (Isset ($port) && strlen ($port) > 0)
Self:: $port = Trim ($port);
}

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

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

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

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

}

/**
* A Database Operations tool class
*
* @author zhjiun@gmail.com
*/
Class Dbtemplate {

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

/**
* Return as 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 single 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 single 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 single field, return 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 single field, return object, actual type has 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;
}
}

/**
* Executes 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 will be used by default in PHP tutorial 5,php6. Unlike the chaotic database operation in previous versions, PDO unifies the access to the database, which brings great convenience to programming. This tool class is based on PDO, simulating the JdbcTemplate operation class in the Java World Spring Framework
*/

http://www.bkjia.com/PHPjc/630760.html www.bkjia.com true http://www.bkjia.com/PHPjc/630760.html techarticle PDO MySQL Tutorial database Tutorial Operations Class * The Dbconfig class is responsible for configuring database access information, including: Server address, port, DB instance name, user name, user password, character set ...

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