PHP数据库编程③基于mysql的在线词典案例(只实现中英文互查)

建表:
id(主键,自增)
enword(vachar,128):添加:boy,girl
chword(vachar,256):添加:男孩,女孩、女生

*******************************************/
主页面:mainView.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>单词翻译</title>
</head>
<body>
<h1>查询英文</h1>
<form action="wordProcess.php" method="post">
请输入英文:<input type="text" name="enword">
<input type="hidden" value="search1" name="type">
<input type="submit" value="查询">
<h1>查询中文</h1>
</form>
<form action="wordProcess.php" method="post">
请输入中文:<input type="text" name="chword">
<input type="hidden" value="search2" name="type">
<input type="submit" value="查询">
</form>
</body>
</html>

********************************************/
处理文件:wordProcess.php

<?php

    require_once 'SQLtool_class.php';
    //接收type
    $type=$_POST['type'];
    if ($type=="search1"){
        //接收英文单词
        if (isset($_POST['enword'])){
            $en_word=$_POST['enword'];
        }else {
            echo "输入英文为空";
            echo "<a href='mainView.php'>返回查询<a>";
        }
        //看看数据库中有没有这个记录
        $sql="select chword from words where enword='".$en_word."' ";
        $sqltool=new SQLtool();
        $res=$sqltool->execute_dql($sql);
        if ($row=mysql_fetch_assoc($res)){
            echo $en_word."----".$row['chword'].'<br>';
            echo "<a href='mainView.php'>返回查询<a>";
        }else {
            echo "没有这个词条".'<br>';
            echo "<a href='mainView.php'>返回查询<a>";
        }
        mysql_free_result($res);
    } else if ($type=="search2"){
        //接收中文单词
        if (isset($_POST['chword'])){
            $ch_word=$_POST['chword'];
        }else {
            echo "输入中文为空";
            echo "<a href='mainView.php'>返回查询<a>";
        }
        //看看数据库中有没有这个记录
        $sql="select enword from words where chword like '%".$ch_word."%'";
        $sqltool=new SQLtool();
        $res=$sqltool->execute_dql($sql);
        if ($row=mysql_fetch_assoc($res)){
            echo $ch_word."----".$row['enword'].'<br>';
            echo "<a href='mainView.php'>返回查询<a>";
        }else {
            echo "没有这个词条".'<br>';
            echo "<a href='mainView.php'>返回查询<a>";
        }
        mysql_free_result($res);



    }

**********************************************/
封装函数类:SQLtool_class.php

<?php

    class SQLtool{
        private $conn;
        private $host="localhost";
        private $user="root";
        private $password="root1142495240";
        private $db="123"; //指定所链接数据库
        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);
        }
        //完成select
        public function execute_dql($sql){
            $res=mysql_query($sql) or die (mysql_error());
            return $res;

        }
        public function execute_dml($sql){
            $b=mysql_query($sql,$this->conn);
            if (!$b){
                return 0;//失败
            }else {
                if (mysql_affected_rows($this->conn)>0){
                    return 1;//表示真的成功
                }else {
                    return 2;//表示没有行数影响
                }
            }
        }
    }

*****************************************************
实际调试,完美运行。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值