使用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
原创粉丝点击