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