温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么用php+mysql实现英汉查询词典的功能

发布时间:2021-08-11 22:53:14 来源:亿速云 阅读:126 作者:chen 栏目:web开发

本篇内容主要讲解“怎么用php+mysql实现英汉查询词典的功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“怎么用php+mysql实现英汉查询词典的功能”吧!

1.建立数据库

create database worddb;

2.创建表

create table words(
    id int auto_increment primary key,
    en_word varchar(128) not null,
    ch_word varchar(256) not null
);

3.插入数据(只是举个例子,不必太计较单词是不是这个意思,英语很渣,又懒得查)

insert into words(en_word,ch_word) values('boy' , '男孩,男人');
insert into words(en_word,ch_word) values('school' , '学校');
insert into words(en_word,ch_word) values('university' , '学校,大学');

4.封装一下sql工具库  SqlTool.class.php

<?php 

    class SqlTool{
        private $conn;
        private $host = "localhost";
        private $user = "root";
        private $password = "root";
        private $db = "worddb";

        /*
            连接数据库的构造方法
        */
        function SqlTool(){
            $this->conn = mysql_connect($this->host , $this->user , $this->password);
            if(!$this->conn){
                die('连接失败'.mysql_error());
            }

            mysql_select_db($this->db,$this->conn);
            mysql_query('set names gbk');
        }

        //select
        function execute_dql($sql){
            $res = mysql_query($sql,$this->conn);
            return $res;
        }

        //insert、update、delete
        function execute_dml($sql){
            $obj = mysql_query($sql,$this->conn);
            echo "添加的id=".mysql_insert_id($this->conn)."成功";
            if(!$obj){
                //return 0;//操作失败
                die('操作失败'.mysql_error());
            }else{
                if(mysql_affected_rows($this->conn)>0){
                    //return 1;//操作成功
                    echo "操作成功";
                }else{
                    //return 2;//行数没有收到影响
                    die('行数没有受影响');
                }
            }
        }   
    }   
?>

到此准备工作完成了,后边的才是重头戏
先搞定查询英文,输出中文。
准备第一个页面  words.php用于查询输入

<DOCTYPE html>
<html>
    <head><title>在线词典查询</title>
        <meta charset = "gbk"/>
    </head>
    <body>
        <img alt="图片加载失败" src="image/7c03087cb9fdc7c2d8a4d8bdc5521ba4.png"><br />
        <h2>查询英文</h2>
        <form action="wordProcess.php" method="post">
            请输入英文:<input type="text" name="en_word"  />
            <input type="hidden" value="search2" name="type" />
            <input type="submit" value="查询" />
        </form>
    </body>
</html>

怎么用php+mysql实现英汉查询词典的功能

下边做提交处理数据:
首先我们获取输入的数据,然后在处理数据库的东西
1.引入SqlTool.class.php包
2.获取输入的数据
3.判断能不能获取的到,能则继续,不能则返回从新查询
4.准备sql语句
5.调用sql工具类里边的查询功能
6.处理结果集:如果可以查询到输出,不能则返回
7.释放资源

<?php
    require_once 'SqlTools.class.php';
    //接收英文单词
        if(isset($_POST['en_word'])){
            $en_word = $_POST['en_word'];
        }else{
            echo "查无结果";
            echo "<a href='words.php'>返回查询页面</a>";
        }
        //sql语句
        $sql = "select * from words where en_word = '".$en_word."' limit 0,1";

        $sqlTool = new SqlTool();
        $res = $sqlTool->execute_dql($sql);
        if($row=mysql_fetch_assoc($res)){
            echo $en_word."的中文意思是:".$row['ch_word'];
        }else{
            echo "没有查到该词条";
            echo "<a href='words.php'>返回查询页面</a>";
        }
                mysql_free_result($res);
?>

输入boy,点击查询
怎么用php+mysql实现英汉查询词典的功能

我们完成了查询英文输出中文,下边完成查询中文输出对应的英文。
首先做一下准备工作
把查询中文的页面也加进去,另外隐藏表单里分开search2和search3

<h2>查询中文</h2>
        <form action="wordProcess.php" method="post">
            请输入中文:<input type="text" name="ch_word"  />
            <input type="hidden" value="search3" name="type" />
            <input type="submit" value="查询" />
        </form>

然后就是重头戏了:
1.接收提交传过来的type值
2.判断一下type值的search是 1 还是 2
3.如果是 1 ,我们就是搞英文,如果是 2 我们就搞中文
4.英文不说了 ,直接剪切进去即可
5.中文部分:
6.接收中文值并判断,和英文部分一样
7.准备sql语句,并调用查询方法(和英文一样,不一样的是sql语句)
8.判断输入的中文数据库里有没有,有则循环输出,没有则返回
9.释放资源

sql语句查询中文比较麻烦,因为一个英文单词可能对应的有好几个中文意思,如果按照英文的查找方法,估计我们查不到,所以我们使用模糊查询-->like的方法

//接收中文单词
        if(isset($_POST['ch_word'])){
            $ch_word = $_POST['ch_word'];
        }else{
            echo "查无结果";
            echo "<a href='words.php'>返回查询页面</a>";
        }
        //sql语句
        $sql = "select en_word from words where ch_word like '%".$ch_word."%'";
        $sqlTool = new SqlTool();
        $res = $sqlTool->execute_dql($sql);

        if(mysql_num_rows($res)){
            echo trim($ch_word)."对应的英文有:";
            while($row=mysql_fetch_assoc($res)){   
                foreach ($row as $val){
                    echo "<br>".$val;
                }
            }
        }else{
            echo "没有查到关于".$ch_word."的词条";
            echo "<br><a href='words.php'>返回重新查询</a>";
        }
        mysql_free_result($res);

查询学校:
怎么用php+mysql实现英汉查询词典的功能

到此,相信大家对“怎么用php+mysql实现英汉查询词典的功能”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI