第092~093讲 php数据库编程④ - 使用mysql扩展库(在线词典)

092_search.php

<!doctype html>
<html lang="en">
 <head>
  <meta charset="UTF-8">
  <meta name="Generator" content="EditPlus®">
  <meta name="Author" content="">
  <meta name="Keywords" content="">
  <meta name="Description" content="">
  <title>在线词典</title>
 </head>
 <body>
    <h4>英-->中</h4>
    <form method="post" action="wordPress.php">
        请输入英文:<input type="text" name="enword"/>
        <input type="hidden" value="searchEn" name="type"/>
        <input type="submit" value="查询"/>
    </form>
    <h4>中-->英</h4>
    <form method="post" action="wordPress.php">
        请输入中文:<input type="text" name="chword"/>
        <input type="hidden" value="searchCh" name="type"/>
        <input type="submit" value="查询"/>
    </form>
 </body>

</html>

wordPress.php

<?php
    header("content-type:text/html;charset=utf-8");
    include 'sqlUtils.php';
    if(!$_REQUEST['type']){
        echo "请选择要查询的是中文还是英文";
        return;
    }
    $type = $_REQUEST['type'];//为什么值写在value 结果却要在type取值
    //echo "value的值".$value;
    $mysql = new MySqlUtil('localhost','root','tmdqobn');
    $mysql->MySqlConn();
    $mysql->selectDB('db100');
    if($type=="searchEn"){ //英文查中文
        //1、接收英文单词
        $en_word = $_REQUEST['enword'];//假如知道传输方式比如 是post过来的可以直接使用$_POST进行接收
        if(!isset($_REQUEST['enword'])){
            echo "请输入英文单词";
            return;
        }

        $sql = "select * from words where enword like'%$en_word%'";
        echo "英文查中文sql ".$sql;
        $res = $mysql->queryDB($sql);
        $rows = $mysql->FindRows($res);
        echo "英文受影响的行数 ".$rows;
        if($rows<=0){
            echo "库中暂无记录";
            return;
        }
        echo "<table border='1px'width='400px'>";
        while($row=mysql_fetch_assoc($res)){
            echo "<tr><td>{$row['enword']}</td><td>{$row['chword']}</td></tr>";//这里$row['enword']外边不加大括号就报错了。尴尬
        }
        echo "</table>";
        /**
        else{
            echo "根据英文查中文不行了";
        }*/
    }else if($type=="searchCh"){//中文查英文
        //1、接收英文单词
        $ch_word = $_REQUEST['chword'];//假如知道传输方式比如 是post过来的可以直接使用$_POST进行接收
        if(!isset($_REQUEST['chword'])){
            echo "请输入中文";
            return;
        }
        $sql = "select * from words where chword like'%$ch_word%'";
        echo "中文查英文sql ".$sql;
        $res = $mysql->queryDB($sql);
        $rows = $mysql->FindRows($res);
        echo "中文受影响的行数 ".$rows;
        if($rows<=0){
            echo "库中暂无记录";
            return;
        }
        echo "<table border='1px'width='400px'>";
        while($row=mysql_fetch_assoc($res)){
            echo "<tr><td>{$row['chword']}</td><td>{$row['enword']}</td></tr>";//这里$row['chword']外边不加大括号就报错了。尴尬
        }
        echo "</table>";
        /**
        else{
            echo "根据中文查英文不行了";
        }*/
    }else if($type=="addEnCh"){
        if(!isset($_REQUEST['chword'])&&!isset($_REQUEST['enword'])){
            echo "添加词库请同时输入中文和英文";
            return;
        }
        $enword = $_REQUEST['enword'];
        $chword = $_REQUEST['chword'];
        $sql = "INSERT INTO WORDS (enword,chword) VALUES ('$enword','$chword');";
        $res = $mysql->queryDB($sql);
        if($res){
            echo "添加词库成功";
        }else{
            echo "添加词库失败";
        }
    }else if($type=="searchAll"){//查询库里所有数据
        $sql = "SELECT * FROM WORDS";
        $res = $mysql->queryDB($sql);
        $rows = $mysql->FindRows($res);
        echo "中文受影响的行数 ".$rows;
        if($rows<=0){
            echo "库中暂无一条记录,请添加";
            return;
        }
        echo "<table border='1px'width='400px'>";
        echo "<tr><td>英文</td><td>中文</td></tr>";
        while($row=mysql_fetch_assoc($res)){
            echo "<tr><td>{$row['enword']}</td><td>{$row['chword']}</td></tr>";//这里$row['chword']外边不加大括号就报错了。尴尬
        }
        echo "</table>";
    }


?>

sqlUtils.php

<?php
    header("content-type:text/html;charset=utf-8");
    class MySqlUtil{
        public $localhost;
        public $hostName;
        public $pwd;
        public $con;
        public $select;
        public function __construct($localhost,$hostName,$pwd){
            $this->localhost = $localhost;
            $this->hostName = $hostName;
            $this->pwd = $pwd;
        }
        public function MySqlConn(){
            $this->con = mysql_connect($this->localhost,$this->hostName,$this->pwd);

            if(!$this->con){
                die("数据库连接出错").mysql_error;
            }
            return $this->con;
        }

        public function selectDB($dbName){
            $select = mysql_select_db($dbName);
            if(!$select){
                die("数据库表连接出错").mysql_error;
            }else{
                mysql_query("set name utf8");
            }
            return $select;
        }

        public function queryDB($sql){
            //echo "语句 ".$sql;
            echo "<br/>";
            $res = mysql_query($sql,$this->con) or die(mysql_errno().mysql_error());
            //echo "插入数据状态 ".$res;
            echo "<br/>";
            return $res;
        }


        public function affected(){
            if(mysql_affected_rows($this->con)>0){
                return 0;
            }else{
                return 1;
            }
        }

        public function FindRows($res){
            return mysql_num_rows($res);
        }
    }
?>

效果

猜你喜欢

转载自blog.csdn.net/u014449096/article/details/81215852