05-在线小词典案例2---中文查找英文

mainView.php

<html>
<head>
<title>在线词典</title>
<meta http-equiv="content-type" content="text/html;charset=utf-8"/>
</head>
<h1>查询英文</h1>
<form action="wordProcess.php" method="post">
请输入英文:<input type="text" name="enword"/>
<input type="hidden" value="search1" name="type"/>
<input type="submit" value="查询"/>
</form>

<h1>查询中文</h1>
<form action="wordProcess.php" method="post">
请输入中文:<input type="text" name="chword"/>
<input type="hidden" value="search2" name="type"/>
<input type="submit" value="查询"/>
</form>
</html>

wordProcess.php

<?php

    require_once "mysqltool.php";
	header("Content-type:text/html;charset=utf-8");

    //接受type
    if(isset($_POST['type'])){
	   $type=$_POST['type'];
	}else{
	   echo "输入为空";
	   echo "<br/><a href='myView.php'>返回重新查询</a>"; 
	}
  

   if($type=="search1"){
			 //接收英文单词
		   if(isset($_POST['enword'])){
			   $en_word=$_POST['enword'];
		   }else{
			   echo "输入为空!";
			   echo "<br/><a href='myView.php'>返回重新查询</a>";
		   }
		   

		   //看看数据库中有没有这条记录
			$sql="select chword from words where enword='".$en_word."' limit 0,1" ;
			//设计表
			/*
			 create database worddb;

			create table words(
			 id int primary key auto_increment,
			 enword varchar(32) not null,
			 chword varchar(256) not null
			  );
			
			 insert into words(enword,chword)values('boy','男孩');
			 insert into words(enword,chword)values('school','学校');
			*/
			//查询(面向对象)
			$sqlTool=new SqlTool();
			$res=$sqlTool->execute_dql($sql);

			if($row=mysql_fetch_assoc($res)){
			   echo $en_word."对应的中文意思是==".$row['chword']; 
			   echo "<br/><a href='mainView.php'>返回重新查询</a>";
			}else{
				
				echo "没有查询到。。。";
				echo "<br/><a href='mainView.php'>返回重新查询</a>";
			}
			mysql_free_result($res);
		   
   
   
   }else if($type=="search2"){
      //接受中文
	  if(isset($_POST['chword'])){
	    $ch_word=$_POST['chword'];
	  }else{
	    echo "输入为空!";
	    echo "<br/><a href='mainView.php'>返回重新查询</a>";
	  }
	  //看看数据库中有没有这条记录
			$sql="select enword from words where chword like '%".$ch_word."%'";

			
			//设计表
			/*
			 create database worddb;

			create table words(
			 id int primary key auto_increment,
			 enword varchar(32) not null,
			 chword varchar(256) not null
			  );
			
			 insert into words(enword,chword)values('boy','男孩');
			 insert into words(enword,chword)values('school','学校');
			*/
			//查询(面向对象)
			$sqlTool=new SqlTool();
			$res=$sqlTool->execute_dql($sql);
			//查询到了
			if(mysql_num_rows($res)!=0){

			while($row=mysql_fetch_assoc($res)){
			   echo "<br/>".$ch_word."对应的英文意思是==".$row['enword']; 
			   
			}
			}else{
				
				echo "没有查询到。。。";
				
			}

			echo "<br/><a href='mainView.php'>返回重新查询</a>";
			mysql_free_result($res);
       
   } 
?>

mysqltool.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 utf8");
	 }
	 //完成select语句
	 function  execute_dql($sql){
		 $res=mysql_query($sql) or die(mysql_error());
		 return $res;
	 }

	 //完成update delete insert操作
	 function  execute_dml($sql){
		 //返回的是一个布尔值
	   $b=mysql_query($sql,$this->conn) or die(mysql_error());
	   if(!$b){
	      return 0;//0表示失败
	   }else{
	      if(mysql_affected_rows($this->conn)>0){
		   return 1;//表示真的成功
		  }else{
		    return 2;//表示没有行数影响
		  }
	   }
	 }
   }
?>





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

会编程的阿强

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

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

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

打赏作者

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

抵扣说明:

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

余额充值