做这个“在线词典查询系统”,起初感觉比较难,一方面是数据库的庞大,另一方面是知识面的广度,几乎用上了所有的知识,要是界面和体验度好的话还必须要用到ajax,但是,在这里为了简化过程,突出后台业务逻辑,数据库数据采用了小部分的模拟数据,没有进行界面优化,所以,在研究起来也就降低了代码的复杂度。
所谓的在线词典查询,就是接收到用户的查询数据,由服务器执行去数据库去找到数据并且返回给客户端的过程,虽然这个过程说起来比较简单,但真正把它做出来还是要费一些时间的。在面是做这个项目的具体过程。
首先要引入自己写的工具类,要实现dql和dml的查询,工具类的代码如下,在我的另一篇博客有具体的介绍:http://blog.csdn.net/mycodedream/article/details/44160431
SqlTool.class.php:
<?php
class SqlTool{
private $conn;
private $host="localhost";
private $user="root";
private $password="toor";
private $db="education";
function SqlTool(){
$this->conn=mysql_connect($this->host,$this->user,$this->password);
if(!$this->conn){
die("对不起,连接数据库失败<br/>错误原因:".mysql_error());
}
mysql_select_db($this->db,$this->conn);//选择数据库
mysql_query("set names utf8");
}
public function execute_dql($sql){
//echo "$sql";
//执行数据库dql语句,即查询操作
$res=mysql_query($sql,$this->conn) or die("查询失败,失败原因".mysql_error());
return $res;
}
//在这里就实现了对CRUD的完全封装
public function execute_dml($sql){
$b=mysql_query($sql,$this->conn);
if(!$b){
//return 0; //运行失败
echo "对不起,操作失败";
}else{
if(mysql_affected_rows($this->conn)>0){
//return 1; //运行成功
echo "操作成功!";
}else{
//return 2; //成功,但没有影响行数
echo "操作成功,但是行数没有受到影响";
}
}
mysql_close($this->conn);//关闭连接
}
}
?>
wordProcess.php:
<?php
require_once "SqlTool.class.php";
header("Content-type: text/html;charset=utf-8");//以utf-8的字符编码返回浏览器
if(isset($_POST['type'])){
$type=$_POST['type'];
}else{
echo "<a href='student_management_view.php'>返回到原查询页面</a>";
die();
}
if($type=="search1"){
if(isset($_POST['enword'])){
$en_word=$_POST['enword'];
}else{
die("对不起,输入为空");
echo "<a href='student_management_view.php'>返回到原查询页面</a>";
die();
}
$sql="select * from words where enword='$en_word'";
$sqlTool=new SqlTool();
$res=$sqlTool->execute_dql($sql);
if(mysql_num_rows($res)!=0){
while($row=mysql_fetch_assoc($res)){
echo $en_word." 该词条的汉语意思为:".$row['chword']."<br/>";
}
}else{
echo "<span style='color: red'>对不起,您输入的词条为空或则没有这个词条</span><br/>";
}
mysql_free_result($res);
}else if($type="search2"){
if(isset($_POST['chword'])){
$ch_word=$_POST['chword'];
}else{
die("对不起,输入为空");
echo "<a href='student_management_view.php'>返回到原查询页面</a>";
die();
}
$sql="select enword from words where chword like '%".$ch_word."%'";
//echo $sql."<br/>";
$sqlTool=new SqlTool();
$res=$sqlTool->execute_dql($sql);
if(mysql_num_rows($res)!=0){
while($row=mysql_fetch_assoc($res)){
echo "<span style='color: red;'>该词条的英文词条为:".$row['enword']."<br/>";
}
}else{
echo "<span style='color: red'>对不起,您输入的词条为空或则没有这个词条</span><br/>";
}
mysql_free_result($res);
}
echo "<a href='student_management_view.php'>返回到原查询页面</a>";
die();
?>
Show.php:
<html>
<head>
<meta charset="utf-8"/>
<title>学生词典管理系统</title>
<LINK href="./picture/logo2.ico" rel="shortcut icon">
</head>
<body bgcolor="#555555">
<h1>学生词典管理系统</h1>
<br/>
<img src="./picture/word.png"/>
<form action="wordprocess.php" method="post">
请输入单词:<input type="text" name="enword"><br/><br/><br/>
<input type="hidden" name="type" value="search1">
<input type="submit" value="查询单词" style="margin-left: 130px;">
</form>
<form action="wordprocess.php" method="post">
请输入汉语:<input type="text" name="chword"><br/><br/><br/>
<input type="hidden" name="type" value="search2">
<input type="submit" value="查询汉语对应的单词" style="margin-left: 110px;">
</form>
</body>
</html>
这个项目的过程重点就在于数据的交换和数据的判断处理,这两点是非常重要的。