PHP language-Encapsulated Connection database class

Source: Internet
Author: User
Tags php language

<?PHPclassdbda{ Public $host= "localhost";//Server Address Public $uid= "Root";//Database user name Public $pwd= "123";//Database Password//Execute SQL statement, return the corresponding result of the function//$sql is to execute the SQL statement//$type is the type of SQL statement, 0 for adding or deleting 1 for the query//$DB represents the database to operatePulicfunctionQuery ($sql,$type=1,$db= "MyDB"){//Connecting Objects$conn=NewMysqli ($this->host,$this->uid,$this->pwd,$db);//determine if the connection is successful!Mysqli_connect_error() or die("Connection Failed");//Execute SQL statement$result=$conn->query ($sql);//determine the type of SQL statementif($type==1){//if it is a query statement, returns a two-dimensional array of result setsreturn $result-Fetch_all ();}Else{//returns TRUE or False if it is a different statementreturn $result;}}}

Single-Condition Query

<body><div><form action= "Current page" method= "POST" >//Create query boxes and buttons<div>Name:<input type= "text" name= "name"/><input type= "submit" value= "Query"/></div></form></div> <br/><table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" ><tr>//Table Header<td> code </td><td> name </td><td> price </td></tr><?PHPinclude("dbda.php");$db=NewDbda ();$str="";if(!Empty($_post["Name"])){$name=$_post["Name"];if($name!="")//the empty string is not empty and needs to be judged{$str=$str. "Where Name like '%{$name}%‘"}}//Write SQL statements$sql= "Select Code,name,price from Car".$str;//invoke the Query method inside the class to execute the SQL statement$attr=$db->query ($sql); for($i= 0;$i<Count($attr);$i++){$attr[$i]Echo"<tr><td>{$attr[$i][0]}</td><td>{$attr[$i][1]}</td><td>{$attr[$i][2]}</td></tr> ";}?></table></body>

Multi-criteria Query and keyword highlighting

<body><div><form action= "Current page" method= "POST" >//Create query boxes and buttons<div>Name:<input type= "text" name= "name"/>Price:<input type = "text" name= "price"/><input type= "submit" value= "Query"/></div></form></div ><br/><table width= "100%" border= "1" cellpadding= "0" cellspacing= "0" ><tr>//Table Header<td> code </td><td> name </td><td> price </td></tr><?PHPinclude("dbda.php");$db=NewDbda (); @$name=$_post["Name"];@$price=$_post["Price"];$TJ 1= "1=1";$TJ 2= "1=1";if($name!=""){$th 1= "Name like '%{$name}%‘";}if($price!=""){$TJ 2= "Price ={$price}";}$str= "where".$TJ 1. "and".$TJ 2;//Write SQL statements$sql= "Select Code,name,price from Car".$str;//invoke the Query method inside the class to execute the SQL statement$attr=$db->query ($sql); for($i= 0;$i<Count($attr);$i++){
Keyword discoloration processing$MC=Str_replace($name, "<span style=" color:red ">{$name}</span> ",$attr[$i][1]);Echo"<tr><td>{$attr[$i][0]}</td><td>{$MC}</td><td>{$attr[$i][2]}</td></tr> ";}?></table></body>

PHP language-Encapsulated Connection database class

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.