Php+mysql realization of the function of English-Chinese query dictionaries

Source: Internet
Author: User
Tags php class

1. Database creation

create database worddb;

2. Create a table

create table words(    id int auto_increment primary key,    en_word varchar(128) not null,    ch_word varchar(256) not null);

3. Insert data (just for example, don't worry too much about the meaning of the word, English is very slag, and too lazy to check)

insert into words(en_word,ch_word) values(‘boy‘ , ‘男孩,男人‘);insert into words(en_word,ch_word) values(‘school‘ , ‘学校‘);insert into words(en_word,ch_word) values(‘university‘ , ‘学校,大学‘);

4. Encapsulate the SQL ToolPak SqlTool.class.php

<?php class sqltool{private $conn;        Private $host = "localhost";        Private $user = "root";        Private $password = "root";        Private $db = "WORDDB"; /* How to construct the connection database */function SqlTool () {$this->conn = mysql_connect ($this->host, $            This->user, $this->password);            if (! $this->conn) {die (' connection failed '. Mysql_error ());            } mysql_select_db ($this->db, $this->conn);        mysql_query (' Set names GBK ');            }//select function Execute_dql ($sql) {$res = mysql_query ($sql, $this->conn);        return $res;            }//insert, update, delete function Execute_dml ($sql) {$obj = mysql_query ($sql, $this->conn); echo "added id=". mysql_insert_id ($this->conn). "            Success ";            if (! $obj) {//return 0;//operation failed die (' operation failed '. Mysql_error ()); }else{if (mysql_afFected_rows ($this->conn) >0) {//return 1;//operation succeeded Echo "Operation succeeded";                }else{//return 2;//Number of rows did not receive impact die (' number of rows not affected '); }}}}?>

The preparation is complete, the back is the play
Check English first, output Chinese.
Prepare the first page words.php for query input

<DOCTYPE html>

Submit processing Data below:
First we get the input data and then the stuff in the processing database
1. Introduction of SqlTool.class.php Package
2. Get the input data
3. To determine whether or not to obtain, to continue, can not be returned from the new query
4. Preparing SQL statements
5. Invoke the query function inside the SQL tool class
6. Working with result sets: If you can query the output, you cannot return
7. Releasing Resources

<?php require_once ‘SqlTools.class.php‘; //接收英文单词 if(isset($_POST[‘en_word‘])){ $en_word = $_POST[‘en_word‘]; }else{ echo "查无结果"; echo "<a href=‘words.php‘>返回查询页面</a>"; } //sql语句 $sql = "select * from words where en_word = ‘".$en_word."‘ limit 0,1"; $sqlTool = new SqlTool(); $res = $sqlTool->execute_dql($sql); if($row=mysql_fetch_assoc($res)){ echo $en_word."的中文意思是:".$row[‘ch_word‘]; }else{ echo "没有查到该词条"; echo "<a href=‘words.php‘>返回查询页面</a>"; } mysql_free_result($res);?>

Enter Boy, click Query

To be continued-----not yet finished

Php+mysql realization of the function of English-Chinese query dictionaries

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.