The encapsulation of MySql dql and dml operations has become a fixed tool class.
When we use php to operate MySQl, the preceding SQL statements are divided into data query statements (dql), data operation statements (dml), and data resource statements.
However, the most important thing we use is the first two. query is the top priority of database operations, and CRUD is essential for database operations. No matter what database language we learn
So, in my daily learning process, each of these two most important statements is encapsulated into the class, conducive to reuse of code
And code conciseness. They are stored in an external file and directly introduced into the file when used. This improves development efficiency. The following is encapsulation.
Specific Code, Note: To express the clear, some data set data, such as the database.
SqlTool. class. php
<? Phpclass SqlTool {private $ conn; private $ host = "localhost"; private $ user = "root"; private $ password = "toor"; private $ db = "test "; function SqlTool () {$ this-> conn = mysql_connect ($ this-> host, $ this-> user, $ this-> password); if (! $ This-> conn) {die ("sorry, database connection failed <br/> error cause :". mysql_error ();} mysql_select_db ($ this-> db, $ this-> conn); // select the database mysql_query ("set names utf8 ");} public function execute_dql ($ SQL) {// execute the database dql statement, that is, query operation $ res = mysql_query ($ SQL) or die ("query failed, cause of failure ". mysql_error (); return $ res;} // The complete CRUD encapsulation public function execute_dml ($ SQL) {$ B = mysql_query ($ SQL, $ this-> conn); if (! $ B) {// return 0; // operation failed echo "sorry, operation failed";} else {if (mysql_affected_rows ($ this-> conn)> 0) {// return 1; // success echo "operation successful! ";}Else {// return 2; // success, but the number of rows is not affected. echo" operation succeeded, but the number of rows is not affected ";}} mysql_close ($ this-> conn); // close the connection }}?>
UseSqlTool. php
<? Phprequire_once "SqlTool. class. php "; // introduce the SqlTool class file ****** * ********************** // Add data $ SQL = "insert into test1 (name, password, email, age) values ('xuning _ a', md5 ('000000'), 'xiaohei @ qq.com ', 18 )"; // SQL statement $ SqlTool = new SqlTool (); $ res = $ SqlTool-> execute_dml ($ SQL ); * // delete data/* $ SQL = "delete from test1 where id = 21"; // SQL statement $ SqlTool = new SqlTool (); $ res = $ SqlTool-> execute_dml ($ SQL );*// /Modify data $ SQL = "update test1 set age = 100 where id = 9"; // SQL statement $ SqlTool = new SqlTool (); $ res = $ SqlTool-> execute_dml ($ SQL ); *************** * ***************** // query operation $ SQL = "select * from test1 "; $ SqlTool = new SqlTool (); $ res = $ SqlTool-> execute_dql ($ SQL); // The user prompt echo "User ID" is displayed ". "". "User Name ". "". "User Password ". "". "email ". "". "Age ". "". "<br/>"; while ($ row = mysql_fetch_row ($ res) {// display all user data foreach ($ row as $ Key => $ val) {echo "$ val". "";} echo "<br/>" ;}mysql_free_result ($ res); */?>
UseSqlTool. introduce SqlTool in php. class. PHP file, and then instantiate the object. What we need to do is write an SQL statement and call it. I think the SQL statement can also be encapsulated, the degree of freedom of the Code is not high. Therefore, we first classify the statements and then write the member methods. What we want to say is that the data can be effectively protected here, only elements in a class can access data, which is convenient and secure.