mysql在线词典_mysql扩展库应用---在线词典程序范例

1,在mysql中创建数据表words。

create table words(

id int primary key not null auto_increment,

enword varchar(32) character set utf8 not null,

chword varchar(32) character set utf8 not null

);

2,编写数据库类,有以下代码可见,数据库类的封装不彻底,比如连接的关闭,需要调用后。自己手动关闭。

class SqlTool {

private $conn = null;

private $host = "127.0.0.1";

private $user = "root";

private $password = "123456";

private $db = "test";

//mysql扩展库操作mysql数据库步骤如下

public function __construct(){

//1.获取连接

$this->conn = mysql_connect($this->host,$this->user,$this->password) or die('连接错误:'.mysql_error());

//2.选择数据库

mysql_select_db($this->db,$this->conn) or die('选择数据库出错:'.mysql_error());

//3.设置操作编码(建议有)

mysql_query("set names utf-8");

}

//对数据表的查询操作

public function execute_dql($sql){

$rs = mysql_query($sql,$this->conn) or die('数据库查询失败:'.mysql_error());

$rsList = array();

if($rs){

while($row = mysql_fetch_assoc($rs)){

$rsList[] = array('id ' => $row['id'], 'enword'=> $row['enword'],'chword'=> $row['chword']);

}

}

mysql_free_result($rs);

return $rsList;

}

//对数据表的增删改操作

public function execute_dml($sql){

$rs = mysql_query($sql,$this->conn);

if(!$rs){

echo '数据库操作失败:'.mysql_error()."\n";

$b = 0; //表示失败

}else{

if(mysql_affected_rows($this->conn) > 0){

$b = 1; //数据表有变动

}else{

$b = 2; //没有影响数据表

}

}

return $b;

}

//关闭conn连接

public function closeConn(){

mysql_close($this->conn);

}

}

3,编写输入页面。

在线词典

查询英文

请输入英文:

4,编写输出页面,调用的查询,查询完毕后,需要关闭资源。

require_once "SqlTool.class.php";

header("Content-type:text/html;charset=utf-8");

$en_word = $_POST['enword'];

if(!isset($en_word)){

echo "输入为空,请点击重新查询";

}

$sql = "select chword from words where enword ='{$en_word}' limit 0,1";

$sqlTool = new SqlTool();

$res = $sqlTool->execute_dql($sql);

if($res && $row=$res[0]){

echo $row['chword'];

}else{

echo "没有合适的翻译!";

}

$sqlTool->closeConn();

5,调用SqlTool.class.php类库,进行增删改操作如下,

require_once "SqlTool.class.php";

header("Content-type:text/html;charset=utf-8");

$sql = "insert into words(enword,chword) VALUES ('hello','你好')";

$sqlTool = new SqlTool();

$res = $sqlTool->execute_dml($sql);

if($res){

echo "操作成功!";

}else{

echo "操作失败!";

}

$sqlTool->closeConn();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值