PHP核心编程(MySQL扩展)
回顾MySQL
MySQL是CS结构的软件
在任务管理器中查看
数据库引擎
innodb:默认情况下所有表数据在一个文件中
myisam:默认情况下每个表都是独立的
如果表文件是独立的,在做数据迁移的时候可以直接拷贝表文件。
MySQL客户端
一:命令行客户端
优点:启动方便,运行速度快
缺点:不是可视化界面
二:phpMyAdmin(web形式的客户端)
优点:只要有浏览器就可以操作数据库
缺点:不能保存没有操作完毕的数据
三:navicate或mysql-front(window界面客户端)
优点:可以保存没有操作完毕的数据
缺点:需要安装才能使用。
四:PHP页面做客户端
将PHP作为MySQL客户端
要使用php链接MySQL必须开启相应的扩展
PHP有三种扩展连接数据库
方式一:mysql扩展
方法二:mysqli扩展
方法三:pdo扩展
连接数据库
mysql_connect(‘主机地址:端口号’,’用户名’,’密码’)
连接失败执行or后面的内容
连接成功返回连接资源
例题
如果端口号是3306,端口号可以省略
选择数据库
方法一:通过mysql_select_db()函数
方法二:通过执行SQL语句
设置字符编码
显示数据
将资源匹配成数组
mysql_fetch_row:将资源中的一条数据匹配成索引数组
将资源中的一条数据匹配成索引数组,并且指针下移一条。
mysql_fetch_assoc:将资源中的一条数据匹配成关联数组
将资源中的一条数据匹配成关联数组,并且指针下移一条。通过字段名做键
mysql_fetch_array:匹配数组中既有关联数组又有索引数组
将资源中的数据匹配成结果数组,此数组中既有关联数组又有索引数组
mysql_fetch_object:匹配成对象
将每一条记录匹配成对象,表中的字段是对象的属性,在PHP中对象访问属性使用->符号。
获取资源的记录数
获取资源的字段数
释放资源
关闭连接
连接数据库完整例题
<?php
//连接数据库
$link=@mysql_connect(‘localhost’,’root’,’root’); //连接数据库
if(!$link){
echo ‘数据库连接失败<br>’;
echo ‘错误信息:’.mysql_error(),'<br>’; //返回上一个MySQL操作产生的文本错误信息
echo ‘错误编码:’.mysql_errno(),'<br>’; //返回上一个MySQL操作中的错误信息的数字编码
exit;
}
//选择数据库
//mysql_select_db(‘jokedb’); //选择数据库(方法一)
mysql_query(‘use jokedb’); //选择数据库方法二
//设置字符编码
mysql_query(‘set names utf8’); //设置字符编码
//获取笑话类别
$rs=mysql_query(‘select * from title’);
//将资源中的数据匹配成索引数组
/*
while($rows=mysql_fetch_row($rs)){
print_r($rows);
echo ‘<br>’;
}
*/
//将资源中的数据匹配成关联数组
/*
while($rows=mysql_fetch_assoc($rs)){
print_r($rows);
echo ‘<br>’;
}
*/
//将资源中的数据匹配成结果数组,此数组中既有关联数组又有索引数组
/*
while($rows=mysql_fetch_array($rs)){
print_r($rows);
echo ‘<br>’;
}
*/
//将资源中的数据匹配成对象
while($rows=mysql_fetch_object($rs)){
echo $rows->Id,’-‘,$rows->Title,'<br>’;
}
//资源中的记录数
echo mysql_num_rows($rs),'<br>’;
//资源中的字段数
echo mysql_num_fields($rs),'<br>’;
//释放资源
mysql_free_result($rs);
//关闭连接
mysql_close();
?>
总结用到的函数
- mysql_connect():连接数据库
- mysql_error():返回上一个MySQL操作产生的文本错误信息
- mysql_errno():返回上一个MySQL操作中的错误信息的数字编码
- mysql_select_db():选择数据库
- mysql_query():执行SQL语句
- mysql_fetch_row():匹配成索引数组
- mysql_fetch_assoc():匹配成关联数组
- mysql_fetch_array():匹配成既有关联数组又有索引数组
- mysql_fetch_object():将资源匹配成对象。
- mysql_num_rows():资源的记录数
- mysql_num_fields():资源的字段数
- mysql_free_result():释放资源
- mysql_close():关闭连接
项目实现——显示笑话
显示笑话类别
显示笑话类别,点击笑话类别传递笑话的编号
<?php
@mysql_connect(‘localhost’,’root’,’root’) or die(mysql_error()); //连接数据库
mysql_query(‘use jokedb’); //选择数据库
mysql_query(‘set names utf8’); //设置字符
?>
<!–显示笑话类别–>
<table>
<tr>
<?php
$rs=mysql_query(‘select * from title’);
$n=0;
while($rows=mysql_fetch_assoc($rs)):
?>
<th><a href=”?titleid=<?php echo $rows[‘Id’]?>”><?php echo $rows[‘Title’]?></a></th>
<?php
if(++$n%9==0)
echo ‘</tr><tr>’;
endwhile;
?>
</tr>
</table>
运行结果
显示笑话内容
运行结果
“显示笑话内容”的完整代码
<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>无标题文档</title>
<style type=”text/css”>
table{
width:980px;
margin:auto;
border:#000 solid 1px;
font-size:12px;
}
td,th{
border:#000 solid 1px;
}
</style>
</head>
<body>
<?php
@mysql_connect(‘localhost’,’root’,’root’) or die(mysql_error()); //连接数据库
mysql_query(‘use jokedb’); //选择数据库
mysql_query(‘set names utf8’); //设置字符
?>
<!–显示笑话类别–>
<table>
<tr>
<?php
$rs=mysql_query(‘select * from title’);
$n=0;
while($rows=mysql_fetch_assoc($rs)):
?>
<th><a href=”?titleid=<?php echo $rows[‘Id’]?>”><?php echo $rows[‘Title’]?></a></th>
<?php
if(++$n%9==0)
echo ‘</tr><tr>’;
endwhile;
?>
</tr>
</table>
<!–显示笑话内容–>
<?php
$titleid=isset($_GET[‘titleid’])?$_GET[‘titleid’]:1; //获取笑话类别
$sql=”select * from contents where title=$titleid order by id desc”; //拼接查询语句
$rs=mysql_query($sql);
?>
<table>
<tr>
<th>编号</th> <th>内容</th> <th>作者</th>
</tr>
<?php
while($rows=mysql_fetch_assoc($rs)):
?>
<tr>
<td><?php echo $rows[‘Id’]?></td>
<td><?php echo $rows[‘Contents’]?></td>
<td><?php echo $rows[‘Author’]?></td>
</tr>
<?php
endwhile;
?>
</table>
</body>
</html>
项目实现——文件包含
css样式代码在每个页面都要使用,在站点下创建style文件夹,在style文件夹中创建home.css样式,此样式为所有页面公用样式
其他页面引入样式文件即可
连接数据代码在项目中要出现多次,为了减少代码冗余,我们将相同的代码做成包含文件。
在站点下创建inc文件夹,在此文件夹中创建conn.php页面,此页面用来连接数据库
其他页面包含conn.php页面即可
项目实现——添加笑话
进入添加笑话页面的入口
显示添加笑话页面
<?php
require ‘./inc/conn.php’;
?>
<form method=”post” action=””>
<table>
<tr>
<th colspan=’2′>添加笑话</th>
</tr>
<tr>
<td>笑话类别</td>
<td>
<select name=”type”>
<?php
$rs=mysql_query(‘select * from title’);
while($rows=mysql_fetch_assoc($rs)):
?>
<option value=”<?php echo $rows[‘Id’]?>”><?php echo $rows[‘Title’]?></option>
<?php
endwhile;
?>
</select>
</td>
</tr>
<tr>
<td>笑话内容</td>
<td><textarea name=’content’ rows=’5′></textarea></td>
</tr>
<tr>
<td>作者</td>
<td><input type=”text” name=”author” id=””></td>
</tr>
<tr>
<td colspan=’2′ align=’center’>
<input type=”submit” value=”添加笑话”>
<input type=”button” value=”返回” onclick=’location.href=”index.php”‘>
</td>
</tr>
</table>
</form>
实现添加笑话业务逻辑
知识点
脚下留心:mysql_query()的返回值
- 执行数据查询语句(select,show)返回数据资源,失败返回false
- 执行数据操作语句(insert,update,delete)成功返回true,失败返回false。
脚下留心:跳转的方式
- 超链接跳转 <a href=’地址’></a>
- Js跳转 location对象
- PHP跳转 header(‘location:地址’);
实现代码
<?php
require ‘./inc/conn.php’;
if(!empty($_POST)){
$titleid=$_POST[‘type’]; //类别id
$content=$_POST[‘content’]; //笑话内容
$author=$_POST[‘author’]; //笑话作者
$sql=”insert into contents (title,contents,author) values ($titleid,’$content’,’$author’)”;
if(mysql_query($sql))
header(‘location:index.php?titleid=’.$titleid); //跳转到index.php页面
else{
echo ‘<div>添加失败</div>’;
}
exit;
}
?>
项目实现——删除笑话
添加删除和修改按钮
<table>
<tr>
<th>编号</th> <th>内容</th> <th>作者</th> <th>删除</th> <th>修改</th>
</tr>
<?php
while($rows=mysql_fetch_assoc($rs)):
?>
<tr>
<td><?php echo $rows[‘Id’]?></td>
<td><?php echo $rows[‘Contents’]?></td>
<td><?php echo $rows[‘Author’]?></td>
<td><input type=”button” value=”删除” onclick=”if(confirm(‘确定要删除吗’))location.href=’del.php?id=<?php echo $rows[‘Id’]?>&titleid=<?php echo $rows[‘Title’]?>'”></td>
<td><input type=”button” value=”修改”> </td>
</tr>
<?php
endwhile;
?>
</table>
运行结果
执行删除的业务逻辑
在站点下新建del.php页面,用来执行删除的业务逻辑
<?php
require ‘./inc/conn.php’;
$id=$_GET[‘id’]; //获取删除的id
$titleid=$_GET[‘titleid’]; //获取删除笑话的类别编号
$sql=”delete from contents where id=$id”;
if(mysql_query($sql))
header(‘location:index.php?titleid=’.$titleid);
else
echo mysql_error();
脚下留心:del.php页面不需要html结构,因为del.php页面只执行业务逻辑,没有显示功能。只要没有显示功能的页面都不需要html结构。
项目实现——修改笑话
修改笑话入口(index.php)
<td><input type=”button” value=”修改” onclick=’location.href=”update.php?id=<?php echo $rows[‘Id’]?>”‘> </td>
显示修改界面
在站点下创建update.php页面
<!doctype html>
<html>
<head>
<meta charset=”utf-8″>
<title>无标题文档</title>
<link rel=’stylesheet’ href=’style/home.css’/>
</head>
<body>
<?php
require ‘./inc/conn.php’;
$id=(int)$_GET[‘id’]; //获取修改的编号
$rs=mysql_query(“select * from contents where id=$id”); //修改笑话信息
if(mysql_num_rows($rs)!=1){
echo ‘访问非法’;
exit;
}
$info=mysql_fetch_assoc($rs); //将需要修改的信息转换成关联数组
?>
<form method=”post” action=””>
<table>
<tr>
<th colspan=’2′>修改笑话</th>
</tr>
<tr>
<td>笑话类别</td>
<td>
<select name=”type”>
<?php
$rs=mysql_query(‘select * from title’);
while($rows=mysql_fetch_assoc($rs)):
?>
<option value=”<?php echo $rows[‘Id’]?>” <?php echo $rows[‘Id’]==$info[‘Title’]?’selected’:”?>><?php echo $rows[‘Title’]?></option>
<?php
endwhile;
?>
</select>
</td>
</tr>
<tr>
<td>笑话内容</td>
<td><textarea name=’content’ rows=’5′><?php echo $info[‘Contents’]?></textarea></td>
</tr>
<tr>
<td>作者</td>
<td><input type=”text” name=”author” value=”<?php echo $info[‘Author’]?>”></td>
</tr>
<tr>
<td colspan=’2′ align=’center’>
<input type=”submit” value=”修改笑话”>
<input type=”button” value=”返回” onclick=’location.href=”index.php”‘>
</td>
</tr>
</table>
</form>
</body>
</html>
运行结果
实现修改的业务逻辑
作业
通过phpmyadmin和mysql-font做数据迁移