PHP uses encapsulated classes to manipulate databases and bulk delete

Source: Internet
Author: User

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 Code

PHP uses encapsulated classes to manipulate databases and bulk delete

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.