03-mysql database crud1.1-encapsulate it into a tool class

Source: Internet
Author: User

Mysqltool. php

<? PHP class sqltool {private $ conn; private $ host = "localhost"; private $ user = "root"; private $ Password = "root"; private $ db = "test "; function sqltool () {$ this-> conn = mysql_connect ($ this-> host, $ this-> User, $ this-> password); If (! $ This-> conn) {die ("failed to connect to Database ". mysql_error ();} mysql_select_db ($ this-> dB, $ this-> conn); mysql_query ("set names utf8 ");} // complete the SELECT statement function execute_dql ($ SQL) {$ res = mysql_query ($ SQL) or die (mysql_error (); return $ res ;} // complete the update Delete Insert operation function execute_dml ($ SQL) {// return a Boolean value $ B = mysql_query ($ SQL, $ this-> conn); If (! $ B) {return 0; // 0 indicates failure} else {If (mysql_affected_rows ($ this-> conn)> 0) {return 1; // indicates that the operation is successful} else {return 2; // indicates that the operation has no effect on the number of rows. }}}?>

Mysqldemo3.php

<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/> <? PHP // introduce the sqltool page require_once "mysqltool. PHP "; *********** * ***************/* $ SQL = "insert into user1 (name, password, email, age) values ('xiaoming ', MD5 ('000000'), 'xiaoming @ sohu.com', 34) "; $ sqltool = new sqltool (); $ res = $ sqltool-> execute_dml ($ SQL); if ($ res = 0) {echo "failed to add! ";} Else if ($ res = 1) {echo" added successfully! ";} Else {echo" no influence on the number of lines! ";} ******** * ********** $ SQL = "select * From user1 "; $ sqltool = new sqltool (); $ res = $ sqltool-> execute_dql ($ SQL); While ($ ROW = mysql_fetch_row ($ res )) {foreach ($ row as $ key => $ Val) {echo "-- $ Val" ;}echo "<br/> ";} // release the resource mysql_free_result ($ res);?>

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.