Php --- database Encapsulation

Source: Internet
Author: User

To save time in the future, a class for Operating SQL statements is encapsulated today, which is saved here for future use.

File Name of this class: SqlTool. class. php

There are two main functions: dql and dml.

See the following source code"

 Conn = mysql_connect ($ this-> host, $ this-> username, $ this-> password); if (! $ This-> conn) {die ("connection failed ". mysql_error ($ this. conn);} mysql_select_db ($ this-> db, $ this-> conn); mysql_query ("set names utf8");} // select function execute_dql ($ SQL) {$ result = mysql_query ($ SQL, $ this-> conn); $ arrTemp = array (); $ counter = 0; while ($ row = mysql_fetch_assoc ($ result )) {$ arrTemp [$ counter] = $ row; $ counter ++;} mysql_free_result ($ result); mysql_close ($ this-> conn); return json_encode ($ ArrTemp);} // complete dml public function execute_dml ($ SQL) {$ res = mysql_query ($ SQL, $ this-> conn); if (! $ Res) {return 0; // execution failed} else {if (mysql_affected_rows ($ this-> conn)> 0) {return 1; // execution successful} else {return 2; // No rows have been affected }}}?>

Demonstrate the usage:

 Execute_dml ($ sqlStr); echo "Result:". $ result1 ."
"; $ SqlStr1 =" select * from student "; $ resultStr = $ sqlTool-> execute_dql ($ sqlStr1); echo" current data:
". $ ResultStr;?>


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.