Encapsulation class
<?PHPclassdbda{ Public $host= "localhost";//Server Address Public $uid= "Root";//User name Public $pwd= "123";//Password Public $dbconnect;//Connection object//Operation database Method//$SQL represents the SQL statement to be executed//$type represents the type of SQL statement, 1 for the query, 2 for the delete//$dbname represents the name of the database to be manipulated//if it is a query, return two-dimensional Array//If the additions and deletions are changed, return True or False functionQuery ($sql,$type=1,$dbname= "MyDB") { //Connecting Objects $this->dbconnect =NewMysqli ($this->host,$this->uid,$this->pwd,$dbname); //Judging if there is an error if(!Mysqli_connect_error()) { //If the connection succeeds, execute the SQL statement $result=$this->dbconnect->query ($sql); //Judging by the type of statement if($type==1) { //returns a two-dimensional array if it is a query statement return $result-Fetch_all (); } Else { //returns TRUE or False if it is a different statement return $result; } } Else { return"Connection Failed! "; } }}
View Code
Bulk Delete
<body>PHP//created Objects$db=NewMysqli ("localhost", "root", "123", "MyDB");//Judging if there is an error!Mysqli_connect_error() or die("Connection Failed! ");//Write SQL statements$sql= "SELECT * FROM Info";//Execute SQL statement$result=$db->query ($sql);//reading Data$attr=$result-Fetch_all ();//iterate through the array, showingforeach($attr as $v){ $sex=$v[2]? " Male ":" Female "; $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]} ' > Delete </a><a href= ' update.php?code={$v[0]} ' > Modify </a></td></tr>";}><tr><td><input type= "checkbox" onclick= "Checkall (This)"/> Select all </td><td><input Type= "Submit" value= "bulk Delete"/></td></tr></table><a href= "add.php" > Add Data </a></ Form></body><script type= "Text/javascript" >functionCheckall (a) {varck = Document.getelementsbyclassname ("CK"); //a.checked; document.getElementById (). RemoveAttribute for(vari=0;i<ck.length;i++) { if(A.checked) {Ck[i]. SetAttribute ("Checked", "checked"); } Else{Ck[i]. RemoveAttribute ("Checked"); } } }</script>view code <? php $ck = $_post ["CK" ]; include ("DBDA.class.php" $db = new Dbda (); foreach ( $ck as $sql =" Delete from Info where Code= ' { $db ->query ( $sql , 0);} header ("location:main.php");
View CodePHP uses encapsulated classes to manipulate databases and bulk delete