单词查询案例

数据库建表

  • 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>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值