第092~093讲 php数据库编程④ - 使用mysql扩展库(在线词典)

本文介绍了一个使用PHP实现的在线词典系统,该系统能够通过输入英文或中文词汇查询对应的翻译,并提供了添加新词汇到词库的功能。系统分为前端页面与后端处理两部分,前端负责接收用户输入并发送请求,后端则处理数据库查询和更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

092_search.php

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>在线词典</title>
 </head>
 <body>
    <h4>英-->中</h4>
    <form method="post" action="wordPress.php">
        请输入英文:<input type="text" name="enword"/>
        <input type="hidden" value="searchEn" name="type"/>
        <input type="submit" value="查询"/>
    </form>
    <h4>中-->英</h4>
    <form method="post" action="wordPress.php">
        请输入中文:<input type="text" name="chword"/>
        <input type="hidden" value="searchCh" name="type"/>
        <input type="submit" value="查询"/>
    </form>
 </body>

</html>

wordPress.php

<?php
    header("content-type:text/html;charset=utf-8");
    include 'sqlUtils.php';
    if(!$_REQUEST['type']){
        echo "请选择要查询的是中文还是英文";
        return;
    }
    $type = $_REQUEST['type'];//为什么值写在value 结果却要在type取值
    //echo "value的值".$value;
    $mysql = new MySqlUtil('localhost','root','tmdqobn');
    $mysql->MySqlConn();
    $mysql->selectDB('db100');
    if($type=="searchEn"){ //英文查中文
        //1、接收英文单词
        $en_word = $_REQUEST['enword'];//假如知道传输方式比如 是post过来的可以直接使用$_POST进行接收
        if(!isset($_REQUEST['enword'])){
            echo "请输入英文单词";
            return;
        }

        $sql = "select * from words where enword like'%$en_word%'";
        echo "英文查中文sql ".$sql;
        $res = $mysql->queryDB($sql);
        $rows = $mysql->FindRows($res);
        echo "英文受影响的行数 ".$rows;
        if($rows<=0){
            echo "库中暂无记录";
            return;
        }
        echo "<table border='1px'width='400px'>";
        while($row=mysql_fetch_assoc($res)){
            echo "<tr><td>{$row['enword']}</td><td>{$row['chword']}</td></tr>";//这里$row['enword']外边不加大括号就报错了。尴尬
        }
        echo "</table>";
        /**
        else{
            echo "根据英文查中文不行了";
        }*/
    }else if($type=="searchCh"){//中文查英文
        //1、接收英文单词
        $ch_word = $_REQUEST['chword'];//假如知道传输方式比如 是post过来的可以直接使用$_POST进行接收
        if(!isset($_REQUEST['chword'])){
            echo "请输入中文";
            return;
        }
        $sql = "select * from words where chword like'%$ch_word%'";
        echo "中文查英文sql ".$sql;
        $res = $mysql->queryDB($sql);
        $rows = $mysql->FindRows($res);
        echo "中文受影响的行数 ".$rows;
        if($rows<=0){
            echo "库中暂无记录";
            return;
        }
        echo "<table border='1px'width='400px'>";
        while($row=mysql_fetch_assoc($res)){
            echo "<tr><td>{$row['chword']}</td><td>{$row['enword']}</td></tr>";//这里$row['chword']外边不加大括号就报错了。尴尬
        }
        echo "</table>";
        /**
        else{
            echo "根据中文查英文不行了";
        }*/
    }else if($type=="addEnCh"){
        if(!isset($_REQUEST['chword'])&&!isset($_REQUEST['enword'])){
            echo "添加词库请同时输入中文和英文";
            return;
        }
        $enword = $_REQUEST['enword'];
        $chword = $_REQUEST['chword'];
        $sql = "INSERT INTO WORDS (enword,chword) VALUES ('$enword','$chword');";
        $res = $mysql->queryDB($sql);
        if($res){
            echo "添加词库成功";
        }else{
            echo "添加词库失败";
        }
    }else if($type=="searchAll"){//查询库里所有数据
        $sql = "SELECT * FROM WORDS";
        $res = $mysql->queryDB($sql);
        $rows = $mysql->FindRows($res);
        echo "中文受影响的行数 ".$rows;
        if($rows<=0){
            echo "库中暂无一条记录,请添加";
            return;
        }
        echo "<table border='1px'width='400px'>";
        echo "<tr><td>英文</td><td>中文</td></tr>";
        while($row=mysql_fetch_assoc($res)){
            echo "<tr><td>{$row['enword']}</td><td>{$row['chword']}</td></tr>";//这里$row['chword']外边不加大括号就报错了。尴尬
        }
        echo "</table>";
    }


?>

sqlUtils.php

<?php
    header("content-type:text/html;charset=utf-8");
    class MySqlUtil{
        public $localhost;
        public $hostName;
        public $pwd;
        public $con;
        public $select;
        public function __construct($localhost,$hostName,$pwd){
            $this->localhost = $localhost;
            $this->hostName = $hostName;
            $this->pwd = $pwd;
        }
        public function MySqlConn(){
            $this->con = mysql_connect($this->localhost,$this->hostName,$this->pwd);

            if(!$this->con){
                die("数据库连接出错").mysql_error;
            }
            return $this->con;
        }

        public function selectDB($dbName){
            $select = mysql_select_db($dbName);
            if(!$select){
                die("数据库表连接出错").mysql_error;
            }else{
                mysql_query("set name utf8");
            }
            return $select;
        }

        public function queryDB($sql){
            //echo "语句 ".$sql;
            echo "<br/>";
            $res = mysql_query($sql,$this->con) or die(mysql_errno().mysql_error());
            //echo "插入数据状态 ".$res;
            echo "<br/>";
            return $res;
        }


        public function affected(){
            if(mysql_affected_rows($this->con)>0){
                return 0;
            }else{
                return 1;
            }
        }

        public function FindRows($res){
            return mysql_num_rows($res);
        }
    }
?>

效果

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

有时有晌

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值