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;?>