程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 《php---mysql簡單應用---在線詞典》

《php---mysql簡單應用---在線詞典》

編輯:關於PHP編程

[html]
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
<title>Insert title here</title> 
</head> 
<body> 
    <form action="search.php" method="post"> 
        請輸入您要查找的英文單詞:<br/> 
        <input type="text" name="enword"/><br/> 
        <input type="submit" value="提交"/><br/> 
    </form> 
</body> 
</html> 

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
 <form action="search.php" method="post">
  請輸入您要查找的英文單詞:<br/>
  <input type="text" name="enword"/><br/>
  <input type="submit" value="提交"/><br/>
 </form>
</body>
</html>[php] view plaincopyprint?
<?php 
 
class DicTool{ 
 
    private $conn; 
    private $host = "localhost"; 
    private $user = "root"; 
    private $password = "123"; 
    private $db = "test"; 
 
    //構造函數,初始化數據,連接數據庫  
    function __construct(){ 
             
        $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"); 
    } 
 
    function findchword($enword){ 
         
        $sql = "select * from words where enword ='".$enword."'limit 0,10"; 
         
        $res = mysql_query($sql,$this->conn) or die(mysql_error()); 
         
        if (!$res){ 
            return 0;//失敗  
        }else{ 
             
            $row = mysql_fetch_assoc($res); 
            return $row['chword']; 
        } 
    } 
 

<?php

class DicTool{

 private $conn;
 private $host = "localhost";
 private $user = "root";
 private $password = "123";
 private $db = "test";

 //構造函數,初始化數據,連接數據庫
 function __construct(){
   
  $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");
 }

 function findchword($enword){
  
  $sql = "select * from words where enword ='".$enword."'limit 0,10";
  
  $res = mysql_query($sql,$this->conn) or die(mysql_error());
  
  if (!$res){
   return 0;//失敗
  }else{
   
   $row = mysql_fetch_assoc($res);
   return $row['chword'];
  }
 }

}
[php]
<?php  
    header("Content-Type: text/html; charset=UTF-8"); 
    include_once 'DicTool.class.php'; 
     
    $dicTool = new DicTool(); 
     
    if (isset($_POST['enword'])){ 
         
        $chword = $dicTool->findchword($_POST['enword']); 
        if ($chword){ 
            echo $_POST['enword']."對應的中文意思是:".$chword."<br/>"; 
        }else{ 
            echo "未查詢到結果!<br/>"; 
        } 
    }else{ 
        echo "未獲取任何輸入!<br/>"; 
    } 
        echo "<a href='find.html'>點擊返回<a/><br/>"; 

<?php
 header("Content-Type: text/html; charset=UTF-8");
 include_once 'DicTool.class.php';
 
 $dicTool = new DicTool();
 
 if (isset($_POST['enword'])){
  
  $chword = $dicTool->findchword($_POST['enword']);
  if ($chword){
   echo $_POST['enword']."對應的中文意思是:".$chword."<br/>";
  }else{
   echo "未查詢到結果!<br/>";
  }
 }else{
  echo "未獲取任何輸入!<br/>";
 }
  echo "<a href='find.html'>點擊返回<a/><br/>";

 

\
 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved