• 在线小词典(mysql扩展库操作)


    • 输入英文查询中文

    1、建表

    create table words(
      id int primary key auto_increment,
      enWords varchar(32) not null,
      chWords varchar(256) not null
    );

    2、插入数据

    insert into words (enWords,chWords) values ('I','我');
    insert into words (enWords,chWords) values ('you','你');
    insert into words (enWords,chWords) values ('he','他');

    表words如下:

    3、程序如下:

    mainView.php  主界面

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4   <title>在线英汉词典</title>
     5 </head>
     6 <body>
     7   <h2>查询英文</h2>
     8   <form action="wordProcess.php" method="post">
     9     请输入英文:<input type="text" name="enWord"/>
    10     <input type="submit" value="查询"/>
    11   </form>
    12 </body>
    13 </html>

    wordProcess.php 处理页面

     1 <?php
     2     //引入操作数据库文件
     3     require_once "mysqlTool.class.php";
     4     //接收用户输入
     5     if(!empty($_POST['enWord'])){
     6         $enWord=$_POST['enWord'];
     7     }else{
     8         echo "输入为空 <a href='mainView.php'>返回重新查询</a>";
     9         die();
    10     }
    11     //查询数据库
    12     $mysqlTool=new MysqlTool();
    13     $sql="select chWords from words where enWords='".$enWord."'limit 0,1";
    14     $res=$mysqlTool->executeDql($sql);
    15     if($row=mysql_fetch_assoc($res)){
    16         echo $enWord."的中文意思是:".$row['chWords'];
    17         echo "<br/><a href='mainView.php'>返回重新查询</a>";
    18     }else{
    19         echo "查不到这个词<br/>";
    20         echo "<a href='mainView.php'>返回重新查询</a>";
    21     }
    22     mysql_free_result($res);
    23     $mysqlTool->mysqlClo();
    24 ?>
    mysql.class.php 数据库处理页面
     1 <?php
     2     class MysqlTool{
     3         private $host="localhost";
     4         private $userName="root";
     5         private $pwd="root";
     6         private $dbName="test";
     7         private $conn;
     8         //连接数据库函数,构造函数(与类同名),实例化后自动调用
     9         public function MysqlTool(){
    10             $this->conn=mysql_connect($this->host,$this->userName,$this->pwd);
    11             if(!$this->conn){
    12                 die("连接数据库失败".mysql_error());
    13             }
    14             mysql_select_db($this->dbName,$this->conn);
    15             mysql_query("set names utf8",$this->conn);
    16         }
    17         //dql语句,完成select
    18         public function executeDql($sql){
    19             $res=mysql_query($sql,$this->conn) or die("操作失败".mysql_error());
    20             return $res;
    21         }
    22         //dml语句,完成insert,delete,update
    23         public function executeDml($sql){
    24             $res=mysql_query($sql,$this->conn);
    25             if(!$res){
    26                 return 0;//0表示操作失败
    27             }else{
    28                 if(mysql_affected_rows($this->conn)>0){
    29                     return 1;//1表示操作成功
    30                 }else{
    31                     return 2;//2表示没有行数影响
    32                 }
    33             }
    34         }
    35         //关闭数据库
    36         public function mysqlClo(){
    37             mysql_close($this->conn);
    38         }
    39     }
    40 ?>

    结果如下:

    情况1——输入单词存在于数据库中

    情况2——没有输入

    情况3——输入单词数据库中没有

    • 中英文互查

    1、向表中插入新的数据如下:

    insert into words (enWords,chWords) values ('your','你们的');

    中文->英文的时候,涉及到模糊查询,sql语句中用到like,用法如下:

    $sql="select enWords from words where chWords like' %".$chWord."% '";

     这样,在输入的中文为“你们”的时候,也能查询到“your”。

    2、程序如下:

    mainView.php 主界面:加入隐藏表单来区分提交的是哪个表单

     1 <!DOCTYPE html>
     2 <html lang="en">
     3 <head>
     4   <title>在线英汉词典</title>
     5 </head>
     6 <body>
     7   <h2>查询英文</h2>
     8   <form action="wordProcess.php" method="post">
     9     <!--加入隐藏表单来区分提交的是哪个表单-->
    10     <input type="hidden" value="enSearch" name="type"/>
    11     请输入英文:<input type="text" name="enWord"/>
    12     <input type="submit" value="查询"/>
    13   </form>
    14   <h2>查询中文</h2>
    15   <form action="wordProcess.php" method="post">
    16     <!--加入隐藏表单来区分提交的是哪个表单-->
    17     <input type="hidden" value="chSearch" name="type"/>
    18     请输入中文:<input type="text" name="chWord"/>
    19     <input type="submit" value="查询"/>
    20   </form>
    21 </body>
    22 </html>

    wordProcess.php  处理页面

     1 <?php
     2     //引入操作数据库文件
     3     require_once "mysqlTool.class.php";
     4     //看提交的哪个表单
     5     if(!empty($_POST['type'])){
     6         $type=$_POST['type'];
     7     }else{
     8         echo "输入为空 <a href='mainView.php'>返回重新查询</a>";
     9         die();
    10     }
    11     //接收用户输入,查询英文意思
    12     if($type=="enSearch"){
    13         if(!empty($_POST['enWord'])){
    14             $enWord=$_POST['enWord'];
    15         }else{
    16             echo "输入为空 <a href='mainView.php'>返回重新查询</a>";
    17             die();
    18         }
    19         //查询数据库
    20         $mysqlTool=new MysqlTool();
    21         $sql="select chWords from words where enWords='".$enWord."'";
    22         $res=$mysqlTool->executeDql($sql);
    23         if($row=mysql_fetch_assoc($res)){
    24             echo $enWord."的中文意思是:".$row['chWords'];
    25             echo "<br/><a href='mainView.php'>返回重新查询</a>";
    26         }else{
    27             echo "查不到这个词<br/>";
    28             echo "<a href='mainView.php'>返回重新查询</a>";
    29         }
    30         mysql_free_result($res);
    31         $mysqlTool->mysqlClo();
    32     }
    33     //接收用户输入,查询中文对应英文
    34     else if($type=="chSearch"){
    35         if(!empty($_POST['chWord'])){
    36             $chWord=$_POST['chWord'];
    37         }else{
    38             echo "输入为空 <a href='mainView.php'>返回重新查询</a>";
    39             die();
    40         }
    41         //查询数据库
    42         $mysqlTool=new MysqlTool();
    43         //中文查询用模糊查询
    44         $sql="select enWords from words where chWords like'%".$chWord."%'";
    45         $res=$mysqlTool->executeDql($sql);
    46         if($row=mysql_fetch_assoc($res)){
    47             echo $chWord."对应的英文单词是:".$row['enWords'];
    48             echo "<br/><a href='mainView.php'>返回重新查询</a>";
    49         }else{
    50             echo "查不到这个词<br/>";
    51             echo "<a href='mainView.php'>返回重新查询</a>";
    52         }
    53         mysql_free_result($res);
    54         $mysqlTool->mysqlClo();
    55     }
    56 ?>

    结果如下:

    情况1:英文->中文

    情况2:中文->英文(模糊查询)

  • 相关阅读:
    C# 中的委托和事件
    sql笔记-group by 统计功能
    js,css小知识点记录
    sql小技巧
    《孙子兵法》总结
    .Net深复制、浅复制
    《君主论》
    php邮箱找回密码功能
    后台管理员账号不能同时登陆,以及登陆使对方强制下线功能
    好程序员应该读的30本书
  • 原文地址:https://www.cnblogs.com/seaBiscuit0922/p/5917820.html
Copyright © 2020-2023  润新知