php+mysql realizes the function of English-Chinese query dictionary

1. Create a database

create database worddb;

2. Create the table

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

3. Insert data (just an example, don't worry too much about the meaning of the word, English is very scum, and I'm too lazy to check)

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. Encapsulate the sql tool library 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('行数没有受影响');
                }
            }
        }   
    }   
?>

At this point, the preparatory work is completed, and the next part is the highlight
. First, get the query in English and output in Chinese.
Prepare the first page words.php for query input

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

php+mysql realizes the function of English-Chinese query dictionary

Next, we will submit and process the data:
first, we obtain the input data, and then process the database things
1. Introduce the SqlTool.class.php package
2. Get the input data
3. Determine whether you can get it, continue if you can, and return if you can't Renew query
4. Prepare sql statement
5. Call the query function in the sql tool class
6. Process the result set: if the output can be queried, return if not
7. Release resources

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

Enter boy, click the query
php+mysql realizes the function of English-Chinese query dictionary
is not finished-----to be continued

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324855483&siteId=291194637