[html]
class DicTool{
private $conn;
private $host = "localhost";
private $user = "root";
private $password = "123";
private $db = "test";
//Constructor, initialize data, connect to database
function __construct(){
$this->conn = mysql_connect($this->host, $this->user, $this->password);
if (!$this->conn){
Die("Failed to connect to database".mysql_error());
}
//Select database
mysql_select_db($this->db,$this->conn);
mysql_query("set names utf8");
}
function findchword($enword){
$sql = "select * from words where enword ='".$enword."'limit 0,10";
$res = mysql_query($sql,$this->conn) or die(mysql_error());
if (!$res){
Return 0;//Failed
}else{
$row = mysql_fetch_assoc($res);
Return $row['chword'];
}
}
}
[php]
header("Content-Type: text/html; charset=UTF-8");
Include_once 'DicTool.class.php';
$dicTool = new DicTool();
If (isset($_POST['enword'])){
$chword = $dicTool->findchword($_POST['enword']);
If ($chword){
echo $_POST['enword']."The corresponding Chinese meaning is: ".$chword."
";
}else{
echo "No results found!
";
}
}else{
echo "No input obtained!
";
}
echo "Click to return
";