[Share] PDO for MySQL operations

Source: Internet
Author: User

Introduction: This is a [Share] PDO operation MySQL class detailed page, introduced and PHP, related knowledge, skills, experience, and some PHP source code.

Class = 'pingjiaf' frameborder = '0' src = 'HTTP: // biancheng.dnbc?info/pingjia.php? Id = 346699 'scrolling = 'no'> reposted from share

Final edit liujijunbd

To maximize the reuse of your data classes, write a reusable PDO class to operate MYSQL:

Because PDO can connect to various popular databases, you can write a configuration class to complete the configuration of different database DSN:

<? PHP/*** class standard description: configuration class for connecting PDO to database * Class Name: configdatabase * Function Description: to reuse code, this class can be used to dynamically connect to various databases * parameter description: $ _ DBMS = "MySQL"; // database type * $ _ host = '2017. 0.0.1 '; // database IP Address * $ _ Port = '000000'; // database port * $ _ username = 'root '; // database username * $ _ password = 'liujijun'; // password * $ _ dbname = 'zendf '; // The default database name is zenf * $ _ charset = 'utf-8'; // The database character encoding * $ _ DSN; /// data soruce Name Data Source ** class attribute description: * class method Description: * return value: different functions return different values * Note: * Author: Liu Ji Jun * last modification time: 2011 02:01:39 */class configdatabase {protected static $ _ DBMS = "MySQL"; // database type protected static $ _ host = '2017. 0.0.1 '; // database IP address protected static $ _ Port = '000000'; // database port protected static $ _ username = 'root '; // database username protected static $ _ password = 'liujijun'; // password protected static $ _ dbname = 'zendf '; // The default database name is zenf protected static $ _ charset = 'utf-8'; // The database character encoding protected static $ _ DSN; // data soruce Name Data Source/*** @ return data source name */public static function getdsn () {// combine the variable values into MYSQL: host = localhost; Port = 3306; dbname = test', $ login, $ passwd format if (! Isset (Self ::$ _ DSN) {self ::$ _ DSN = self ::$ _ DBMS. ': host = '. SELF: $ _ host. '; prot = '. SELF: $ _ port. '; dbname = '. SELF: $ _ dbname. ','. SELF: $ _ username. ','. SELF: $ _ password; If (strlen (SELF: $ _ charset)> 0) {self: $ _ DSN = self ::$ _ DSN. '; charset = '. SELF: $ _ charset;} return self: $ _ DSN; // return data source name}/*** function: set $ DBMS * @ Param $ DBMS */public static function setdbms ($ DBMS) {If (isset ($ DBMS) & (Str Len ($ 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 */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 database operations: <? Phprequire_once 'configdatabase. PHP '; header ("Content-Type: text/html; charset = UTF-8"); // sets encoding/*** class standard description * Class Name: pdomysql * function description: perform Various operations on the database * parameter description: * class attribute description: * class method Description: * return value: * Note Description: * Author: Liu Ji Jun * last modification time: 2011 10:45:36 am **/class pdomysqloperater {/*** @ return returns the database connection handle */Public Function getconnection () {$ connection = NULL; try {$ connection = new PDO (configdatabase: getdsn (); echo 'success';} Cat CH (pdoexception $ e) {print "error in connection :". $ e-> getmessage (). ''. die ();} return $ connection;}/***** @ Param $ connection handle connecting to the 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 statement */Public Function insertdatabase ($ SQL) {$ affect = false; // if a failure occurs, false is returned. Try {$ conn = $ this-> getconnection (); $ Conn-> exec ($ SQL); $ affect = true; // return true $ this-> closeconnection ($ conn) after successful insertion; // closes the database} catch (pdoexception $ e) {print 'insert Error '. $ e-> getmessage ();} return $ affect; // return value}/***** @ Param $ id Primary Key ID of the 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 the record * @ Param $ tablename table name * @ Param $ array the field name in the array table = its value in combination */Public Function preparedeleteand ($ tablename, array $ array = NULL) {$ SQL = 'delete from '. $ tablename. 'where'; $ COUNT = count ($ array); // calculate the length of the array $ flag = 0; // set the flag foreach ($ array as $ key => $ value) {$ flag ++; // Add $ SQL to the flag. = $ key. '= '. "'". $ value. "'"; if ($ flag! = $ Count) {// when falg is not equal to count, the array also has a value, followed by and, and vice versa, no $ SQL is added. = 'and' ;}} echo $ SQL; // test the SQL statement combination try {$ conn = $ this-> getconnection (); // get the connection $ Conn-> prepare ($ SQL); $ this-> closeconnection ();} catch (pdoexception $ e) {print 'delete error is '. $ e-> getmessage () ;}/ *** function: delete the record * @ Param $ tablename table name * @ Param $ array the field name in the array table = its value in combination */Public Function preparedeleteor ($ tablename, array $ array = nu Ll) {$ SQL = 'delete from '. $ tablename. 'where'; $ COUNT = count ($ array); // calculate the length of the array $ flag = 0; // set the flag foreach ($ array as $ key => $ value) {$ flag ++; // Add $ SQL to the flag. = $ key. '= '. "'". $ value. "'"; if ($ flag! = $ Count) {// when falg is not equal to count, the array also has a value, followed by and, and vice versa, no $ SQL is added. = 'or' ;}} echo $ SQL; // test the SQL statement combination try {$ conn = $ this-> getconnection (); // get the connection $ stmt = $ Conn-> prepare ($ SQL); $ stmt-> execute (); // execute $ this-> closeconnection ();} catch (pdoexception $ e) {print 'delete error is '. $ e-> getmessage () ;}}/*** function: obtain all data in the table * @ Param $ SQL statement */Public Function getall ($ SQL) {$ result = NULL; try {$ conn = $ this-> GETC Onnection (); $ result = $ Conn-> query ($ SQL); $ this-> closeconnection ($ conn);} catch (pdoexception $ E) {print 'getall error is '. $ e-> getmessage () ;}/ *** function: update the information in the data table * @ Param $ name of the table to be updated * @ Param array $ updatefiled field to be updated * @ Param array $ updateconditon conditions required for updating */Public Function updatedatabase ($ table, array $ updatefiled, array $ updateconditon) {$ SQL = 'Update from '. $ table. 'set'; // perform the SET field Value assignment operation $ COUNT = count ($ updatefiled); // obtain the length of the array to be modified $ flag = 0; // set to 0 foreach ($ updatefiled as $ key => $ value) {$ flag ++; $ SQL. = $ key. '= '. "'". $ value. "'"; if ($ flag! = $ Count) {$ SQL. = ',' ;}/// assign a value to the where condition $ countupdatecondition = count ($ updateconditon); // obtain the length of the array to be modified $ flag = 0; // set to 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 results based on tables and improved query conditions * return values: returned result set * @ Param $ table data table name * @ Param array $ findcondition query condition */Public Function finddata ($ table, array $ findcondition) {$ SQL = 'select from '. $ table. 'Where '; $ COUNT = count ($ findcondition); // obtain the length of the query condition array $ flag = 0; // set to 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' => 'liujijun '));

Love J2EE follow Java Michael Jackson video station JSON online tools

Http://biancheng.dnbcw.info/php/346699.html pageno: 4.

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.