单词查询案例

数据库建表

  • mysql -hlocalhost -uroot -p
  • 输入密码:********
  • show databases; → 查看数据库
  • use 数据库名; → 使用某个数据库
  • show tables; → 查看该数据库下有哪些表
  • 创建表
create table words(
	id int primary key auto_increment,
	enword varchar(64) not null,
	chword varchar(128) not null
)ENGINE=InnoDB DEFAULT CHARSET=utf8;
  • 插入数据
insert into words values(1,'one','一');
insert into words values(2,'two','二');
insert into words values(3,'words','单词');

php程序

  • 界面
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
<title>在线词典</title>
</head>
<body>
<img src="1.jpg" width="100px"/>
<form action="process.php" method="post">
请输入英文单词:<input type="text" name="enword"/>
<input type="hidden" value="search1" name="submitType"/>
<input type="submit" value="查询"/>
</form>
<form action="process.php" method="post">
请输入中文:<input type="text" name="chword"/>
<input type="hidden" value="search2" name="submitType"/>
<input type="submit" value="查询"/>
</form>
</body>
</html>

在这里插入图片描述

  • 处理程序(process.php)
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<?php
	require_once "sqlToolDefine.class.php";
	//接收的类型
	if(isset($_REQUEST['submitType'])){
		$submitType=$_REQUEST['submitType'];
	}else{
		echo "输入为空!<br/>";
		echo "<a href='process.php'>返回重新查询</a>";
	}
	if($submitType=="search1"){
		if(isset($_REQUEST['enword'])){
			$en_word=$_REQUEST['enword'];
			//注意语句的书写,尤其是变量的单引号!!!
			$sql="select * from words where enword='$en_word' limit 0,1"; //查找到一条记录则停止
			$sqltool=new sqlToolDefine("test");
			$res=$sqltool->execute_dql($sql);
			if($res!="no"){
				echo $en_word."→".$res."<br/>";
			}else{
				echo "无此单词<br/>";
			}
			echo "<a href='process.php'>返回重新查询</a>";
			$sqltool->close();
		}else{
			echo "输入为空!<br/>";
			echo "<a href='process.php'>返回重新查询</a>";
		}
	}else if($submitType=="search2"){
		if(isset($_REQUEST['chword'])){
			$ch_word=$_REQUEST['chword'];
			$sql="select * from words where chword like '%$ch_word%' limit 0,1"; //查找到一条记录则停止
			$sqltool=new sqlToolDefine("test");
			$res=$sqltool->execute_dql2($sql);
			if($res!="no"){
				echo $ch_word."→".$res."<br/>";
			}else{
				echo "无此中文<br/>";
			}
			echo "<a href='process.php'>返回重新查询</a>";
			$sqltool->close();	
		}else{
			echo "输入为空!<br/>";
			echo "<a href='process.php'>返回重新查询</a>";
		}
	}
?>
</body>
</html>
  • 封装类(sqlToolDefine.class.php)
<html>
<head>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<body>
<?php
	class sqlToolDefine{
		private $conn;
		private $host="localhost";
		private $user="root";
		private $password="********";
		function __construct($db){
			$this->conn=new mysqli($this->host,$this->user,$this->password);
			if($this->conn->connect_error){
				die("连接失败".$this->conn->connect_error);
			}
			$this->conn->select_db($db);
			$this->conn->query("set names utf-8");
		}
		//select
		public function execute_dql($sql){
			$res=$this->conn->query($sql);
			//var_dump($res);
			if($row=$res->fetch_row()){
				//$row是一个数组
				//var_dump($row);
				mysqli_free_result($res);
				return $row[2];
			}else{
				return "no";
			}
		}
		public function execute_dql2($sql){
			$res=$this->conn->query($sql);
			//var_dump($res);
			if($row=$res->fetch_row()){
				//$row是一个数组
				//var_dump($row);
				mysqli_free_result($res);
				return $row[1];
			}else{
				return "no";
			}
		}
		//update,delete,insert
		public function execute_dml($sql){
			$res=$this->conn->query($sql);
			if(!$res){
				return 0; //失败
			}else{
				if(mysqli_affected_rows($this->conn)>0){
					return 1; //表示成功
				}else{
					return 2; //表示没有此行
				}
			}
		}
		public function close(){
			mysqli_close($this->conn);
		}
	}
	
?>
</body>
</html>
©️2020 CSDN 皮肤主题: 技术工厂 设计师:CSDN官方博客 返回首页