標籤:
封裝類
<?phpclass DBDA{ public $host="localhost"; //伺服器位址 public $uid="root"; //使用者名稱 public $pwd="123"; //密碼 public $dbconnect; //連線物件 //操作資料庫的方法 //$sql代表需要執行的SQL語句 //$type代表SQL語句的類型,1代表查詢,2代表增刪改 //$dbname代表要操作的資料庫名稱 //如果是查詢,返回二維數組 //如果是增刪改,返回true或false function Query($sql,$type=1,$dbname="mydb") { //造連線物件 $this->dbconnect = new MySQLi($this->host,$this->uid,$this->pwd,$dbname); //判斷是否出錯 if(!mysqli_connect_error()) { //如果串連成功,執行SQL語句 $result = $this->dbconnect->query($sql); //根據語句類型判斷 if($type==1) { //如果是查詢語句,返回二維數組 return $result->fetch_all(); } else { //如果是其他語句,返回true或false return $result; } } else { return "串連失敗!"; } }}
View Code
大量刪除
<body><h1>首頁面</h1><form action="piliangshanchu.php" method="post"><table width="100%" border="1" cellpadding="0" cellspacing="0"><tr> <td>代號</td> <td>姓名</td> <td>性別</td> <td>民族</td> <td>生日</td> <td>操作</td></tr><?php//造對象$db = new MySQLi("localhost","root","123","mydb");//判斷是否出錯!mysqli_connect_error() or die("串連失敗!");//寫SQL語句$sql = "select * from Info";//執行SQL語句$result = $db->query($sql);//讀取資料$attr = $result->fetch_all();//遍曆數組,顯示foreach($attr as $v){ $sex = $v[2]?"男":"女"; $sql = "select Name from Nation where Code = ‘{$v[3]}‘"; $r = $db->query($sql); $a = $r->fetch_row(); echo " <tr> <td> <input class=‘ck‘ name=‘ck[]‘ type=‘checkbox‘ value=‘{$v[0]}‘ /> {$v[0]}</td> <td>{$v[1]}</td> <td>{$sex}</td> <td>{$a[0]}</td> <td>{$v[4]}</td> <td><a href=‘delete.php?code={$v[0]}‘>刪除</a><a href=‘update.php?code={$v[0]}‘>修改</a></td></tr> ";}?><tr><td><input type="checkbox" onclick="CheckAll(this)" />全選</td><td><input type="submit" value="大量刪除" /></td></tr></table><a href="add.php">添加資料</a></form></body><script type="text/javascript">function CheckAll(a){ var ck = document.getElementsByClassName("ck"); //a.checked; //document.getElementById().removeAttribute for(var i=0;i<ck.length;i++) { if(a.checked) { ck[i].setAttribute("checked","checked"); } else { ck[i].removeAttribute("checked"); } } }</script></html>
View Code
<?php$ck = $_POST["ck"];include("DBDA.class.php");$db = new DBDA();foreach($ck as $v){ $sql = "delete from Info where Code=‘{$v}‘"; $db->Query($sql,0);}header("location:main.php");
View Code
php 用封裝類的方法操作資料庫和大量刪除