建表:
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;//表示没有行数影响
}
}
}
}
*****************************************************、
实际调试,完美运行。