標籤:
1 <?php 2 3 /** 4 * 函數名稱:SqlTool.class.php 5 * 函數功能:php對資料庫增刪改查操作類 6 * 函數張真貴 7 * 建立時間:2015-01-05 8 * 修改時間: 9 */10 header("Content-Type:text/html;charset=utf-8");11 class SqlTool{12 private $conn;13 private $host = ‘localhost‘;14 private $root = ‘root‘;15 private $password = ‘‘;16 private $dbname = test;17 18 function __construct(){19 # code...20 $this->conn = mysql_connect($this->host,$this->root,$this->password) or die(‘串連資料庫失敗‘.mysql_error());21 mysql_select_db($this->dbname);22 mysql_set_charset(‘utf8‘);23 }24 25 /***************************dql操作***********************************/26 public function execute_dql($sql){27 $res = mysql_query($sql,$this->conn) or die(mysql_error());28 return $res;29 }30 31 /****************************dml操作***********************************/32 public function execute_dml($sql){33 $res = mysql_query($sql,$this->conn) or die(mysql_error());34 if (!$res) {35 # code...36 return 0;37 }elseif (mysql_affected_rows($this->conn) > 0) {38 # code...39 return 1;40 }else{41 # code...42 return 2;43 }44 }45 }46 47 /*******************48 $sql = "insert into user1(id,name) values(‘7‘,‘陸遜‘)";49 //建立對象50 $sqlTool = new SqlTool;51 $result = $sqlTool-> execute_dml($sql);52 if ($result == 0) {53 # code...54 echo "失敗";55 }elseif ($result == 1) {56 # code...57 echo "成功";58 }elseif ($result == 2) {59 # code...60 echo "沒有影響行數";61 }62 *******************/ 63 ?>
php對資料庫增刪改查操作類