一.SQL语言基础
1) 关于database的库操作
1.show databases; 可以用来显示所有库名,如果要显示特定的库,可以使用show databases like ' ……' ; 引号中使用通配符来进行对内容的删选,通配符起代替其他字符作用,相当于省略号,常用的通配符有 % 和 _ 两种,其中%可以代替一个或多个字符,包括空字符,而 _ 只能代替一个字符且不能是空字符。例如对于名字为 test 的库,可以用' %t ' , ' t% ' , ' %test% ' , '_est ' , ' t_s_ ' 这些通配符查找到,但是'_test_'是不可以的,因为_不能代替空字符,另外值得注意的是库名是大小写敏感的。
create table st_info(st_id int primary key,st_name varchar(20) unique key, area varchar(20)); //建表时直接设置主键和唯一索引
alter table st_info add primary key (st_id);//设置主键alter table st_info add unique key (st_name);//设置唯一索引alter table st_info drop primary key; //取消主键alter table st_info drop index st_name; //取消某个字段的唯一索引使用了replace into 语句后,如果写如的数据在主键和唯一索引字段中没有和以前的数据重复的,就会直接添加这条数据(普通索引可以重复,也就是说如果表没有主键和唯一索引那么replace 与insert效果相同);如果和主键或唯一索引字段中的某个旧数据重复,就会先删除含有重复数据的旧的数据,然后把添加这条新数据(如果一条数据主键重复,另一条数据唯一索引重复,则删除两条数据,添加一条数据,也就是说replace可能使数据行数减少)
3.查询数据:select 字段1,字段2 from 表名; 可以用来查询某几个字段的所有数据,如果要查询所有字段的所有数据可以用 select * from 表名; 如果要查询部分数据,可以在后面添加where条件,如 select * from st_info where id<5;
二.PHP访问MySQL初步
1) 连接MySQL服务器<?php
$link=mysql_connect('localhost','root','root')or die("连接数据库服务器错误!" .mysql_error());
//mysql_error()用来返回上一个mysql语句产生错误文本信息
if($link){
echo "数据连接成功!";
}
?>2) 关闭MySQL服务器
<?php
$link=mysql_connect("localhost","root","password");
mysql_select_db(database,$link);
mysql_query("use database");
mysql_query("set names gbk");
$result=mysql_query("select * from table");
$info=mysql_fetch_array($result);
if($_POST[Submit]=="查询"){
$txt=$_POST[txt];
$result=mysql_query("select * from table where name like '%" .$txt. "%'");
$info=mysql_fetch_array($result);
$num=mysql_num_rows($result);
if($info==false){
echo"不存在!";
}
}
do{
?>
<tr>
<td><?php echo $info[id] ?></td>
<td><?php echo $info[name] ?></td>
</tr>
<?php
}
while($info=mysql_fetch_array($result));
if($num!=0){
echo"找到条".$num."记录";
}
?>5.利用update实现动态编辑,修改删除与添加查询稍有不同,因为要进行编辑时要根据id来选择编辑哪条记录,所以要多一个传id的动作,可以简便的通过URL传递id再用$_GET[id]接收,全部可以分为三个页,显示页,修改界面业和功能页。
显示页:
<table width="500" cellpadding="0" cellspacing="0" border="0" align="center" >
<tr>
<td align="center">查询公告信息</td>
</tr>
<tr><td height="20"></td></tr>
<tr>
<form name="form_search" method="post" action="">
<td>
查询关键字 <input type="text" name="txt_search" id="txt_search" size="40"> <input type="submit" name="sub_search" value="搜索">
</td>
</form>
</tr>
<tr><td height="20"></td></tr>
<tr>
<td>
<table cellpadding="0" cellspacing="0" border="1" width="480" align="center">
<tr align="center">
<td>公共标题</td>
<td>公告内容</td>
<td>编辑</td>
</tr>
<?php
mysql_connect("localhost","root","root")or die("数据库服务连接错误!".mysql_error());
mysql_query("use database");
mysql_query("set names gbk");
$result=mysql_query("select * from tb_affiche");
$info=mysql_fetch_array($result);
if($_POST[sub_search]=="搜索"){
$txt_search=$_POST[txt_search];
$result=mysql_query("select * from tb_affiche where title like '%". $txt_search ."%'");
$info=mysql_fetch_array($result);
}
do{
?>
<tr>
<td> <?php echo $info[title] ?> </td>
<td> <?php echo $info[content] ?> </td>
<td>
<a href="modify.php ? id=<?php echo $info[id]; ?> ">//在<a>中通过url传递id值,需要用$_GET[id]接收
<img src="update.gif" border="0">
</a>
</td>
</tr>
<?php
}
while($info=mysql_fetch_array($result));
?>
</table>
</td>
</tr>
</table>
<?php
mysql_connect("localhost","root","root")or die("数据库服务连接错误!".mysql_error());
mysql_query("use database");
mysql_query("set names gbk");
$id=$_GET[id];
$result=mysql_query("select*from tb_affiche where id=$id");
$info=mysql_fetch_array($result);
?>
<form name="form" method="post" action="modify_ok.php">
<table width="500" cellpadding="0" cellspacing="0" border="0">
<tr>
<td align="center">编辑公告信息</td>
</tr>
<tr>
<td>
公告主题:<input type="text" name="txt_title" id="txt_title" size="40" >
<input type="hidden" name="id" value=" <?php echo $info[id]; ?>">//隐藏域,用于通过post来传递id值,但这里不能直接用value="$info[id]",因为不在php语句中
</td>
</tr>
<tr>
<td>
公告内容 <textarea name="txt_content" > <?php echo $info[content] ?> </textarea>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="修改">
</td>
</tr>
</table>
</form>修改功能界面:
<?php
$link=mysql_connect("localhost","root","root")or die("连接失败".mysql_error());
mysql_select_db("database",$link);
mysql_query("set names gbk");
$txt_title=$_POST[txt_title];//通过post分别获取txt_text,txt_content和id的值
$txt_content=$_POST[txt_content];
$id=$_POST[id];
$result=mysql_query("update tb_affiche set title=$txt_title, content=$txt_content where id=$id");
if($result!=false){
echo " <script> alert('修改成功'); window.location.href='index.php' </script> ";
}
?>6.分页算法:首先算法有如下几个变量,每页显示的记录数$page_size,由设计者自行规定;记录总数$total,可以用select count(*) from table;来获取总的记录数,当然得到的是一个结果集标识符,还需要mysql_fetch_array()或者mysql_result()转换;页面总数$page_count= $total / $page_size,得到的可能是一个浮点数,再用ceil()转换成整数;最后一个,也是最总要的一个参数,$offset,它表示的是本页面(倒序)从第几个记录开始显示,首先我们显示这些记录用的select语句为
select * from table order by id desc limit $offset, $page_size; 请注意limit后面的两个参数,第一个参数表示从第几个记录开始搜索,从0开始;第二个参数表示一次列出几个记录,也就是说limit 0,1 是显示第一个(倒序后,也就是id最大的那个),limit 1,1是显示第二个。
我们假设共有16个记录,每页能显示4个记录,一共有4页,页数分别1,2,3,4,我们可以得到如下表格(序号=16-1-id ,从0开始,id从1开始)
| 页号 | 1 | 2 | 3 | 4 |
| limit的两个参数 | 0,4 | 4,4 | 8,4 | 12,4 |
| 序号 | 0 | 4 | 8 | 12 |
| 序号 | 1 | 5 | 9 | 13 |
| 序号 | 2 | 6 | 10 | 14 |
| 序号 | 3 | 7 | 11 | 15 |
根据这个表格我们可以很轻易得出limit的第一个参数$offset 与当前页号$page 之间的关系,即$offset = ($page-1) * $page_size,这是分页算法的关键。
得到$offset后利用select * from table order by id desc limit $offset , $page_size ;即可得到本页面所有的记录的结果集,一一显示即可。下一步是制作上一页和下一页的超链接分页条,在大部分页面中都要显示首页,上一页,下一页,尾页这四个超链接,只有在首页只显示下一页和尾页,在尾页只显示首页和上一页,而这两者又是互补的,所以可以巧妙的利用两个if语句,即当不为首页时显示首页和上一页,当不为尾页时显示下一页和尾页,即可完成所有判断工作。另外,虽然返回的依然是这个界面,但在<a>中需要传递新的page值,这样才能改变显示的记录,新page值用$_GET[page]接收,这样便完成了分页的效果。
以下为分页代码:
<table width="500" cellpadding="0" cellspacing="0" border="1" align="center" style="text-align:center">
<tr align="center">
<td colspan="2" height="30">学生信息分页显示</td>
</tr>
<tr align="center" bgcolor="#EBEEF3">
<td>姓名</td>
<td>性别</td>
</tr>
<?php
$link=mysql_connect("localhost","root","root")or die("服务器连接失败!".mysql_error);
mysql_select_db("db_database18",$link)or die("数据库连接失败!".mysql_error);
mysql_query("set names gbk");
$page=$_GET[page];//接收分页条链接url中传来的新page值
if($page==""){$page=1;}//如果第一次打开,page默认值为1
$page_size=2;
$result_total=mysql_query("select count(*) as total from tb_member");//返回值为total=总记录数,as是别名
$total=mysql_result($result_total,0,"total");//返回一个数据,第一个参数结果集标识符,第三个为字段名(列),第二个表示行数(从0开始)
$page_count=ceil($total/$page_size);//ceel()是得到大于等于浮点数的一个整数
$offset=($page-1)*$page_size;//得到limit的一个参数,分析见上
$sql=mysql_query("select * from tb_member order by M_ID desc limit $offset,$page_size");
$info=mysql_fetch_array($sql);
do{
?>
<tr>
<td> <?php echo $info[M_Name]; ?></td>
<td> <?php echo $info[M_Sex]; ?></td>
</tr>
<?php
}
while($info=mysql_fetch_array($sql));//逐行显示
?>
<table width="500" cellspacing="0" cellpadding="0" border="0" align="center">
<tr><td height="10"><td><tr>
<tr style="font-size:13px" >
<td width="40%" style="text-align:center">
<?php echo "页次:".$page."/".$page_count."页 记录:".$total."条"; ?>
<td style="text-align:right">
<?php
if($page!=1){
echo"<a href=index.php?page=1>首页</a> ";//通过URL传递新的page值
$page_last=$page-1;
echo"<a href=index.php?page=$page_last>上一页</a> ";
}
if($page!=$page_count){
$page_next=$page+1;
echo "<a href=index.php?page=$page_next>下一页</a> ";
echo "<a href=index.php?page=$page_count>尾页</a>";
}
?>
</td>
</td>
</tr>
</table>
这篇博客介绍了如何使用PHP连接MySQL数据库,并详细讲解了如何实现动态编辑和分页算法。在动态编辑中,通过URL传递ID进行记录的修改。在分页算法部分,讲述了如何计算每页记录数、总页数以及如何使用LIMIT获取指定范围的记录,同时给出了分页链接的实现方法。
1117

被折叠的 条评论
为什么被折叠?



