To enable your data classes to maximize reuse, write a reusable PDO to manipulate MySQL classes:
Since PDO can connect to the various database tutorials that are now popular, write a separate configuration class class to complete the configuration of different database DSNs
<?php Tutorial
/**
* Class standard description PDO connection Database configuration class
* Class Name: Configdatabase
* Function Description: In order to make code reuse, this class can be used to dynamically connect various databases
* Parameter description: $_dbms = "MySQL tutorial"; Database type
* $_host = ' 127.0.0.1 '; Database IP Address
* $_port = ' 3306 '; Database port
* $_username = ' root '; Database user Name
* $_password = ' Liujijun '; Password
* $_dbname = ' zendf '; Database name defaults to ZENF
* $_charset = ' utf-8 '; Database character encoding
* $_dsn;////data soruce name Data source
*
*
* Class Attribute Description:
* Class Method Description:
* Return value: Different functions return different values
* Note notes:
* Author: Liu Jichun
* Last Modification Time: 2011 02:01:39
*
*/
Class Configdatabase {
protected static $_dbms = "MySQL"; Database type
protected static $_host = ' 127.0.0.1 '; Database IP Address
protected static $_port = ' 3306 '; Database port
protected static $_username = ' root '; Database user Name
protected static $_password = ' Liujijun '; Password
protected static $_dbname = ' zendf '; Database name defaults to ZENF
protected static $_charset = ' Utf-8 '; Database character encoding
protected static $_dsn;////data soruce name Data source
/**
* @return return Data source name
*/
public static function Getdsn () {
Combine the value of a variable into Mysql:host=localhost;port =3306;dbname=test ', $login, $passwd form
if (!isset (SELF::$_DSN)) {
SELF::$_DSN = Self::$_dbms. ': Host = '. Self::$_host '; Prot = '.
Self::$_port. ';d bname = '. Self::$_dbname. ', '.
Self::$_username. ', '. Self::$_password;
if (strlen (Self::$_charset) > 0) {
SELF::$_DSN = Self::$_dsn. '; charset = '. Self::$_charset;
}
}
Return self::$_dsn;//Data Source name
}
/**
* Function: Set $dbms
* @param $dbms
*/
public static function Setdbms ($dbms) {
if (Isset ($dbms) && (strlen ($dbms) > 0)) {
Self::$_dbms = Trim ($dbms);
}
}
/**
*
* @param $host//Database address
*/
public static function Sethost ($host) {
if (Isset ($host) && (strlen ($host) > 0)) {
Self::$_host = Trim ($host);
}
}
/**
*
* @param $host Port number
*/
public static function Setport ($port) {
if (Isset ($port) && (strlen ($port) > 0)) {
Self::$_post = Trim ($port);
}
}
/**
*
* @param $passwd Password
*/
public static function setpasswd ($PASSWD) {
if (Isset ($passwd) && (strlen ($PASSWD) > 0)) {
Self::$_password = Trim ($passwd);
}
}
/**
*
* @param $username User Name
*/
public static function Setusernname ($username) {
if (Isset ($username) && (strlen ($username) > 0)) {
Self::$_username = Trim ($username);
}
}
/**
*
* @param $dbname database name
*/
public static function Setdbname ($dbname) {
if (Isset ($dbname) && (strlen ($dbname) > 0)) {
Self::$_dbname = Trim ($dbname);
}
}
/**
*
* @param $charset Database encoding
*/
public static function Setcharset ($charset) {
if (Isset ($charset) && (strlen ($charset) > 0)) {
Self::$_charset = Trim ($charset);
}
}
}
The following are actions on the database:
<?php
Require_once ' configdatabase.php ';
Header ("content-type:text/html; Charset=utf-8 ");/Set encoding
/**
* Class Standard description
* Class Name: Pdomysql
* Function Description: A variety of operations on the database
* Parameter Description:
* Class Attribute Description:
* Class Method Description:
* Return value:
* Note notes:
* Author: Liu Jichun
* Last Modification Time: 2011 10:45:36
*
*/
Class pdomysqloperater{
/**
* @return return the handle to the connection database
*/
Public Function getconnection () {
$connection = NULL;
try {
$connection = new PDO (CONFIGDATABASE::GETDSN ());
Echo ' Success ';
catch (Pdoexception $e) {
Print "Error in connection:". $e->getmessage (). ' '. Die ();
}
return $connection;
}
/**
*
* @param $connection A handle to the connection database
*/
Public Function CloseConnection ($connection) {
try {
if ($connection!= null) {
$connection = null;//Closes the database connection handle
}
catch (Exception $e) {
print ' Close the Connectin is error: '. $e->getmessage ();
}
}
/**
* Function: Add data to the database
* @param $sql SQL statements
*/
Public Function InsertDatabase ($sql) {
$affect = false;//failed return false
try {
$conn = $this->getconnection ();
$conn->exec ($sql);
$affect = true;//Insert Successfully returns True
$this->closeconnection ($conn);//Close Database
catch (Pdoexception $e) {
print ' Insert error '. $e->getmessage ();
}
return $affect//returned value
}
/**
*
* @param the primary key ID of the $id table
* @param $tableName Table name
*/
Public Function Deleltbyid ($id, $tableName) {
$affact = false;
$sql = ' Delete from '. Trim ($tableName). ' WHERE id = '. $id;
try {
$conn = $this->getconnection ();
$conn->exec ($sql);
$this->closeconnection ($conn);
$affact = true;
catch (Pdoexception $e) {
print ' Delelte error is '. $e->getmessage ();
}
return $affact;
}
/**
* Function: Delete records AS and
* @param the name of the $tableName table
* @param a combination of the field name = its value $array the array table
*/
Public Function Preparedeleteand ($tableName, array $array =null) {
$sql = ' Delete from '. $tableName. ' Where ';
$count = count ($array);//Calculate the length of an array
$flag = 0;//Set tag
foreach ($array as $key => $value) {
$flag ++;//Let flag add a
$sql. = $key. ' ='."'". $value. "'";
if ($flag!= $count) {//when FALG is not equal to count, the array has a value, followed by an and, not increased
$sql. = ' and ';
& nbsp; }
}
echo $sql//combination of test SQL statements
try {
$conn = $this- >getconnection ()//Get connection
$conn->prepare ($sql);
$this->closeconnection ();
catch (Pdoexception $e) {
print ' Delete error is '. $e->getmessage ();
}
}
/**
* Function: Delete records as or
* @param the name of the $tableName table
* @param a combination of the field name = its value $array the array table
*/
Public Function Preparedeleteor ($tableName, array $array =null) {
$sql = ' Delete from '. $tableName. ' Where ';
$count = count ($array);//Calculate the length of an array
$flag = 0;//Set tag
foreach ($array as $key => $value) {
$flag ++;//let flag add a
$sql. = $key. ' ='."'". $value. "'";
if ($flag!= $count) {//when FALG is not equal to count, the array also has a value, followed by an and, not increased
$sql. = ' or ';
}
}
echo $sql//combination of test SQL statements
try {
$conn = $this->getconnection ();//Get connection
$stmt = $conn->prepare ($sql);
$stmt->execute ()//execution
$this->closeconnection ();
catch (Pdoexception $e) {
print ' Delete error is '. $e->getmessage ();
}
}
/**
* Function: Get all the data in the table
* @param $sql SQL statements
*/
Public Function GetAll ($sql) {
$result = null;
try {
$conn = $this->getconnection ();
$result = $conn->query ($sql);
$this->closeconnection ($conn);
catch (Pdoexception $e) {
print ' GetAll error is '. $e->getmessage ();
}
}
/**
* Function: Update the information in the datasheet
* @param $table The name of the table to be updated
* @param array $updateFiled the field to update
* @param array $updateConditon update required conditions
*/
Public Function Updatedatabase ($table, array $updateFiled, array $updateConditon) {
$sql = ' update from '. $table. ' Set ';
Assigning Operations to set fields
$count = count ($updateFiled);//Get the length of the array to modify
$flag = 0;//setting is marked 0
foreach ($updateFiled as $key => $value) {
$flag + +;
$sql. = $key. ' ='."'". $value. "'";
if ($flag!= $count) {
$sql. = ', ';
}
}
assigning to a Where condition
$countUpdateCondition = count ($updateConditon);//Get the length of the array to modify
$flag = 0;//setting is marked 0
$sql. = ' where ';
foreach ($updateConditon as $key => $value) {
$flag + +;
$sql. = $key. ' ='."'". $value. "'";
if ($flag!= $countUpdateCondition) {
$sql. = ' and ';
}
}
try {
$conn = $this->getconnection ();
$conn->exec ($sql);
$this->closeconnection ($conn);
catch (Pdoexception $e) {
print ' Update error is: '. $e->getmessage ();
}
}
/**
* Function: Query according to the table and the improved query condition
* Return value: Returns the result set
* @param $table data table name
* @param array $findCondition query criteria
*/
Public Function FindData ($table, array $findCondition) {
$sql = ' Select from '. $table. ' WHERE ';
$count = count ($findCondition);//Get the length of the array of query criteria
$flag = 0;//setting is marked 0
foreach ($findCondition as $key => $value) {
$flag + +;
$sql. = $key. ' ='."'". $value. "'";
if ($flag!= $count) {
$sql. = ' and ';
}
}
try {
$conn = $this->getconnection ();
$conn->exec ($sql);
$this->closeconnection ($conn);
catch (Pdoexception $e) {
print ' Find error is: '. $e->getmessage ();
}
}
}
Test
$db = new Pdomysqloperater ();
$db->finddata (' Liujijun ', Array (' name ' => ' Liujijun ', ' name1 ' => '));