<?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