建表:
id(主鍵,自增)
enword(vachar,128):添加:boy,girl
chword(vachar,256):添加:男孩,女孩、女生
*******************************************/
主頁面:mainView.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>單詞翻譯</title>
</head>
<body>
<h1>查詢英文</h1>
<form action="wordProcess.php" method="post">
請輸入英文:<input type="text" name="enword">
<input type="hidden" value="search1" name="type">
<input type="submit" value="查詢">
<h1>查詢中文</h1>
</form>
<form action="wordProcess.php" method="post">
請輸入中文:<input type="text" name="chword">
<input type="hidden" value="search2" name="type">
<input type="submit" value="查詢">
</form>
</body>
</html>
********************************************/
處理文件:wordProcess.php
<?php
require_once 'SQLtool_class.php';
//接收type
$type=$_POST['type'];
if ($type=="search1"){
//接收英文單詞
if (isset($_POST['enword'])){
$en_word=$_POST['enword'];
}else {
echo "輸入英文爲空";
echo "<a href='mainView.php'>返回查詢<a>";
}
//看看數據庫中有沒有這個記錄
$sql="select chword from words where enword='".$en_word."' ";
$sqltool=new SQLtool();
$res=$sqltool->execute_dql($sql);
if ($row=mysql_fetch_assoc($res)){
echo $en_word."----".$row['chword'].'<br>';
echo "<a href='mainView.php'>返回查詢<a>";
}else {
echo "沒有這個詞條".'<br>';
echo "<a href='mainView.php'>返回查詢<a>";
}
mysql_free_result($res);
} else if ($type=="search2"){
//接收中文單詞
if (isset($_POST['chword'])){
$ch_word=$_POST['chword'];
}else {
echo "輸入中文爲空";
echo "<a href='mainView.php'>返回查詢<a>";
}
//看看數據庫中有沒有這個記錄
$sql="select enword from words where chword like '%".$ch_word."%'";
$sqltool=new SQLtool();
$res=$sqltool->execute_dql($sql);
if ($row=mysql_fetch_assoc($res)){
echo $ch_word."----".$row['enword'].'<br>';
echo "<a href='mainView.php'>返回查詢<a>";
}else {
echo "沒有這個詞條".'<br>';
echo "<a href='mainView.php'>返回查詢<a>";
}
mysql_free_result($res);
}
**********************************************/
封裝函數類:SQLtool_class.php
<?php
class SQLtool{
private $conn;
private $host="localhost";
private $user="root";
private $password="root1142495240";
private $db="123"; //指定所鏈接數據庫
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);
}
//完成select
public function execute_dql($sql){
$res=mysql_query($sql) or die (mysql_error());
return $res;
}
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn);
if (!$b){
return 0;//失敗
}else {
if (mysql_affected_rows($this->conn)>0){
return 1;//表示真的成功
}else {
return 2;//表示沒有行數影響
}
}
}
}
*****************************************************、
實際調試,完美運行。