在线小词典(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:中文->英文(模糊查詢)
?
轉載于:https://www.cnblogs.com/seaBiscuit0922/p/5917820.html
總結
以上是生活随笔為你收集整理的在线小词典(mysql扩展库操作)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【poj2983】 Is the Inf
- 下一篇: Spring依赖注入:注解注入总结