使用PHP实现简易词典查询功能
来源:互联网 发布:java实现aes加密解密 编辑:程序博客网 时间:2024/05/01 17:17
一、首先 查询界面 代码:
<html><head><title>在线词典</title><meta http-equiv="content-type" content="text/html;charset=utf8"/></head><img src="cidian.png"/><h1>查询英文</h1><form action="worldProcess.php" method="post" > 请输入英文:<input type="text" name = "enword"/> <input type="hidden" value="search" name="type" /> <input type="submit" value="查询" /></form><h1>查询中文</h1><form action="worldProcess.php" method="post" > 请输入中文:<input type="text" name = "chword"/> <input type="hidden" value="search1" name="type" /> <input type="submit" value="查询" /></form></html>
效果如图:
二、创建数据库 表 以及插入 数据
//创建个数据库 create database word; use word; //设计表 英文-》中文 中文-》英文 create table words( id int primary key auto_increment, enword varchar(32) not null, chword varchar(256) not null ); set names gbk; insert into words(enword,chword) values('boy','男孩'); insert into words(enword,chword) values('school','学校'); insert into words(enword,chword) values('word','词语,说话,消息'); insert into words(enword,chword) values('speek','说话,谈话');
三、封装 数据库操作工具类 SqlTools.class.php:
<?php class SqlTools{ private $conn; private $host = "localhost"; private $user = "root"; private $passwd = "xxxxxx"; private $db = "test"; private $res = null; /* function SqlTools(){ $this->conn = mysql_connect($this->host,$this->user,$this->passwd); if(!$this->conn){ die("链接数据库失败!".mysql_error()); return; } mysql_select_db($this->db,$this->conn); mysql_query("set names utf8"); } */ function SqlTools($host,$user,$passwd,$db){ $this->host = $host; $this->user = $user; $this->passwd = $passwd; $this->db = $db; $this->conn = mysql_connect($host,$user,$passwd); if(!$this->conn){ die("链接数据库失败!".mysql_error()); return; } mysql_select_db($this->db,$this->conn); mysql_query("set names utf8"); } //查询 function execute_dql($sql){ $this->res = mysql_query($sql,$this->conn) or die(mysql_error()); return $this->res; } //增删改 function execute_dml($sql){ $b = mysql_query($sql,$this->conn); if(!$b){ return 0;//操作失败 }else{ if(mysql_affected_rows($this->conn)> 0){ echo "添加的id=".mysql_insert_id($this->conn); return 1;//表示操作成功 }else{ return 2;//表示没有改变 } } } //关闭资源 和 链接 function cloase_res(){ } //获得表头 用->name方法获取 function show_tab_head_field(){ //$res = $this->execute_dql($sql); if($this->res <> null){ $headField = mysql_fetch_field($this->res); return $headField; } } //获取表 数据行 function show_tab_lines(){ if($this->conn){ $rows = mysql_affected_rows($this->conn); return $rows; } } //获取表 数据列 function show_tab_colums(){ if($this->res <> null){ $colums = mysql_num_fields($this->res); return $colums; } } }?>
四、对数据库操作的主逻辑 wordProgress.php
<?php header("Content-type:text/html;charset=utf-8"); //初始化工具类 require_once "SqlTools.class.php"; $tools = new SqlTools('localhost','root','159357','word'); $res = null; //接受type if(isset($_POST['type'])){ $type = $_POST['type']; }else{ echo "输入为空!"; echo "<a href='mainView.php'>返回重新</a>"; return; } if($type == "search"){ //接受英文单词 if(isset($_POST['enword']) && $_POST['enword'] <> ''){ $en_word = $_POST['enword']; }else{ echo "输入为空!"; echo "<a href='mainView.php'>返回重新</a>"; return; } //查找数据库 $sql = "select chword from words where enword='$en_word' limit 0,1"; $res = $tools->execute_dql($sql);//查出一条数据 if($row = mysql_fetch_assoc($res)){ echo $en_word."对应的中文意思是--".$row['chword']; }else{ echo "没有这个词条!"; echo "<a href='mainView.php'>返回重新</a>"; return; } }else if($type == "search1"){ //接受中单词 if(isset($_POST['chword']) && $_POST['chword'] <> ''){ $ch_word = $_POST['chword']; }else{ echo "输入为空!"; echo "<a href='mainView.php'>返回重新</a>"; return; } //查找数据库 $sql = "select enword from words where chword like '%".$ch_word."%'"; $res = $tools->execute_dql($sql);//查出一条数据 if(mysql_num_rows($res)==0){ echo "没有这个词条!"; echo "<a href='mainView.php'>返回重新</a>"; return; } while($row = mysql_fetch_assoc($res)){ echo $ch_word."对应的英文意思是--".$row['enword']."<br/>"; } } if($res <> null){ mysql_free_result($res); } echo "<a href='mainView.php'>返回重新</a>";?>
0 0
- 使用PHP实现简易词典查询功能
- HttpGet方式调用有道词典API实现单词查询功能
- 词典查询的实现
- php实现简易的搜索功能
- 简易词典
- php实现自动获取生成文章主题关键词功能-简易分词
- PHP do {} while(false); 用法,实现简易goto功能
- php实现上传功能并保存(简易版)
- PHP实现MYSQL的查询功能
- solr5.3 实现同义词 扩展词典 停止词典 功能介绍
- 实现简易秒表功能
- 实现简易版@功能
- 使用PHP实现密保卡功能
- 实现一个英文词典的功能
- Python实现的命令行查询有道词典
- DWR使用:文字提示功能实现、简易聊天室
- 使用mvc模型实现简易新闻客户端功能(本地)
- PHP实现简易计算器
- linux编程学习2-重要的概念
- Test
- 嵌入式软件开发培训笔记——linux进程、线程及进程间通信
- Javascript Object.defineProperty()
- 求职-如何伪装一份cs的简历
- 使用PHP实现简易词典查询功能
- 输出Bag中的所有记录为一个json数组---精通android、IOS App应用服务程序开发
- 性能更好的js动画实现方式——requestAnimationFrame
- JS中stopPropagation函数停止事件传播
- 家地方规定咖啡馆
- LDA 主题模型解析
- 嵌入式软件开发培训笔记——Linux下SQLite3数据库编程
- Python 3 -Functions
- 虚拟机中 CentOS 网络配置图解