1. 程式人生 > >php+mysql實現英漢查詢詞典的功能

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 />
        <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>

技術分享圖片

下邊做提交處理數據:
首先我們獲取輸入的數據,然後在處理數據庫的東西
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實現英漢查詢詞典的功能