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,编写数据库类,有以下代码可见,数据库类的封装不彻底,比如连接的关闭,需要调用后。自己手动关闭。
<?php
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,编写输入页面。
<html>
<head>
<title>在线词典</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312" />
</head>
<body>
<h1>查询英文</h1>
<form action="onlineDictionary2.php" method="post">
请输入英文:<input type="text" name="enword" />
<input type="hidden" value="search" name="type" />
<input type="submit" value="查询" />
</form>
</body>
</html>
4,编写输出页面,调用的查询,查询完毕后,需要关闭资源。
<?php
require_once "SqlTool.class.php";
header("Content-type:text/html;charset=utf-8");
$en_word = $_POST['enword'];
if(!isset($en_word)){
echo "输入为空,请<a href='onlineDictionary1.php'>点击</a>重新查询";
} $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类库,进行增删改操作如下,
<?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();
mysql扩展库应用---在线词典程序范例的更多相关文章
- mysql扩展库-1
启用mysql扩展库 在php.ini文件中去配置mysql扩展库 extension=php_mysql.dll 可以通过 phpinfo() 查看当前php支持什么扩展库. 在sql扩展库中创建一 ...
- php笔记08:数据库编程---使用php的MySQL扩展库操作MySQL数据库
1.使用php的MySQL扩展库操作MySQL数据库: php有3种方式操作MySQL数据库 (1)mysql扩展库 (2)mysqli扩展库 (3)pdo mysql扩展库与mysql数据库 ...
- php使用mysql扩展库链接mysql数据库(查询)
php链接数据库可以使用mysql扩展库,mysqli,pdo这几种方式,相比java而言要麻烦一点,因为它不像java那么统一.从代码的难易程度来说php的确要简单许多.步骤大体如下 1.打开数据库 ...
- php数据库编程---mysql扩展库
1, Java有一种方式操作数据库, PHP有三种方式来操作mysql数据库.(1)mysql扩展库:(2)mysqli扩展库:(3)pdo: 2, mysql扩展库和mysql数据库区别 3, my ...
- PHP基础Mysql扩展库
mysql扩展库操作步骤如下: 1.连接数据库 2.选择数据库 3.设置操作编码 4.发送指令sql,并返回结果集 ddl:数据定义语句 dml:数据操作语句 dql:数据查询 ...
- mysql扩展库操作mysql数据库
环境搭建 启用mysql扩展库,在php.ini文件中去配置mysql扩展库 extension=php_mysql.dll 查询数据库 1.建库建表 //建库testcreate database ...
- 【小结】有关mysql扩展库和mysqli扩展库的crud操作封装
现阶段php如果要操作mysql数据库 php给我们提供了3套库 1.mysql扩展库 面向过程操作 2.mysqli扩展库 面向对象操作和面向过程操作并存 安全性和效率高于mysql扩展库 ...
- PHP mysql 扩展库 操作mysql数据库步骤
一.mysql 扩展库操作mysql数据库步骤如下: 1.获取连接 2.选择数据库 3.设置操作编码 4.发送指令(ddl数据定义/dml数据操作/dql数据查询/dtl数据事务控制) 5.接收返回的 ...
- 在线小词典(mysql扩展库操作)
输入英文查询中文 1.建表 create table words( id int primary key auto_increment, enWords varchar(32) not null, c ...
随机推荐
- 微信小程序相关二、css介绍,菜单制作,表单相关,京东注册页面
一.第二天上午 1.1上午因为有其他的课所以没有去这个课,不过下午看复习的时候的概括,讲了DTD,语法特性,css选择器以及权重,还有一些简单的样式 1.2 DTD Docuement Type 声明 ...
- Vertex Lit
[Vertex Lit] Vertex Lit path generally renders each object in one pass, with lighting from all light ...
- Java网络编程小结 URLConnection协议处理器
URL和URLConnection类 网络中的URL(Uniform Resource Locator)是统一资源定位符的简称.它表示Internet上某一资源的地址.通过URL我们可以访问Inter ...
- SQLServerDBA十大必备工具
曾经和一些DBA和数据库开发人员交流时,问他们都用过一些什么样的DB方面的工具,大部分人除了SSMS和Profile之外,基本就没有使用过其他工具了: 诚然,SSMS和Profile足够强大,工作的大 ...
- c语言和设计模式
在网上看到一个博客专门写了关于设计模式的文章,感觉很有用.其实,我感觉数据结构 算法 设计模式 这三样同等重要啊. 数据结构 算法相对而言接触的比较多,但设计模式这个东西真的一头雾水,有时候觉得别人写 ...
- Redis中redis.conf里面配置详解
是否将redis设置为守护程序,默认为no daemonize yes 如果设置为守护程序,需要指定pid文件 pidfile /var/run/redis/redis-server.pid ...
- [C++] 2D Array's memory allocation
2D Array's memory allocation
- code1225 八数码Bfs
Bfs搜索 1.把棋盘直接作为状态: #include<iostream> #include<cstring> #include<queue> #include&l ...
- tp5 select回显
<select name="role_id" id="" class="form-control" required> {vol ...
- [SoapUI] JsonPath is to JSON what XPath is to XML
1.通过JsonUtil验证Json的有效性 2.两种方式通过JPath读取Json的内容