1、连接MYSQL数据库代码
<?php
$connec=mysql_connect("localhost","root","root") or die("不能连接数据库服务器: ".mysql_error());
mysql_select_db("liuyanben",$connec) or die ("不能选择数据库: ".mysql_error());
mysql_query("set names 'gbk'");
?>
2、读取数据库,并实现循环输出
<?php
$sql="select * from liuyan order by ly_id desc";
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){
?>
循环的内容.........
<?php
}
?>
3、如何实现分页,包括两个函数,两个调用
1)两个函数
<?
//分页函数
function genpage(&$sql,$page_size=2)
{
global $prepage,$nextpage,$pages,$sums; //out param
$page = $_GET["page"];
$eachpage = $page_size;
$pagesql = strstr($sql," from ");
$pagesql = "select count(*) as ids ".$pagesql;
$conn = mysql_query($pagesql) or die(mysql_error());
if($rs = mysql_fetch_array($conn)) $sums = $rs[0];
$pages = ceil(($sums-0.5)/$eachpage)-1;
$pages = $pages>=0?$pages:0;
$prepage = ($page>0)?$page-1:0;
$nextpage = ($page<$pages)?$page+1pages;
$startpos = $page*$eachpage;
$sql .=" limit $startpos,$eachpage ";
}
//显示分页
function showpage()
{
global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
$shownum =10/2;
$startpage = ($page>=$shownum)?$page-$shownum:0;
$endpage = ($page+$shownum<=$pages)?$page+$shownumpages;
echo "共".($pages+1)."页: ";
if($page>0)echo "<a href=$PHP_SELF?page=0$queryString>首页</a>";
if($startpage>0)
echo " ... <b><a href=$PHP_SELF?page=".($page-$shownum*2)."$queryString>?</a></b>";
for($i=$startpage;$i<=$endpage;$i++)
{
if($i==$page) echo " <b>[".($i+1)."]</b> ";
else echo " <a href=$PHP_SELF?page=$i$queryString>".($i+1)."</a> ";
}
if($endpage<$pages)
echo "<b><a href=$PHP_SELF?page=".($page+$shownum*2)."$queryString>?</a></b> ... ";
if($page<$pages)
echo "<a href=$PHP_SELF?page=$pages$queryString>尾页</a>";
}
//显示带分类的分页
function showpage1()
{
$fenlei=$_GET["fenleiid"];
global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
$shownum =10/2;
$startpage = ($page>=$shownum)?$page-$shownum:0;
$endpage = ($page+$shownum<=$pages)?$page+$shownumpages;
echo "共".($pages+1)."页: ";
if($page>0)echo "<a href=$PHP_SELF?fenleiid=$fenlei&page=0$queryString>首页</a>";
if($startpage>0)
echo " ... <b><a href=$PHP_SELF?fenleiid=$fenlei&page=".($page-$shownum*2)."$queryString>?</a></b>";
for($i=$startpage;$i<=$endpage;$i++)
{
if($i==$page) echo " <b>[".($i+1)."]</b> ";
else echo " <a href=$PHP_SELF?fenleiid=$fenlei&page=$i$queryString>".($i+1)."</a> ";
}
if($endpage<$pages)
echo "<b><a href=$PHP_SELF?fenleiid=$fenlei&page=".($page+$shownum*2)."$queryString>?</a></b> ... ";
if($page<$pages)
echo "<a href=$PHP_SELF?fenleiid=$fenlei&page=$pages$queryString>尾页</a>";
}
?>
2)两个调用
第一个
<?php
$sql="select * from liuyan order by ly_id desc";
genpage($sql); //只需要正常代码加上这一行就ok。
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){
?>
第二个
<?php
}
?>
<?php
showpage(); //显示页
?>
<?php
mysql_close();
?>
4、服务器端包含
<?php require_once('conn.php'); ?>
5、如何将一条记录写入数据库,然后提示并跳转页面
<?php
$ly_title=$_POST["ly_title"];
$ly_content=$_POST["ly_content"];
$ly_time=$_POST["ly_time"];
$ly_author=$_POST["ly_author"];
$ly_email=$_POST["ly_email"];
$sql="insert into liuyan(ly_title,ly_content,ly_time,ly_author,ly_email) values('".$ly_title."','".$ly_content."','".$ly_time."','".$ly_author."','".$ly_email."')";
mysql_query($sql,$connec);
echo("<script type='text/javascript'> alert('添加成功!');location.href='index.php';</script>");
?>
6、弹出对话框,并发生页面跳转
<?php
echo("<script type='text/javascript'> alert('添加成功!');location.href='index.php';</script>");
?>
7、信息查看页面(有条件读取数据库)
1)有条件读取数据库
<?php
$sql="select * from liuyan where ly_id=$_GET[id]";
$conn=mysql_query($sql,$connec);
$rs=mysql_fetch_array($conn);
?>
2)将某个字段输出
<?=$rs[ly_title]?>
3)关闭数据库
<?php
mysql_close();
?>
8、对数据库中某一条记录进行更新操作,并作提示跳转
<?php
$ly_title=$_POST["ly_title"];
$ly_content=$_POST["ly_content"];
$ly_time=$_POST["ly_time"];
$ly_author=$_POST["ly_author"];
$ly_email=$_POST["ly_email"];
$sql="update liuyan set ly_title='$ly_title',ly_content='$ly_content',ly_time='$ly_time',ly_author='$ly_author',ly_email='$ly_email' where ly_id=$_GET[id]";
mysql_query($sql,$connec);
echo("<script type='text/javascript'> alert('更新成功!');location.href='../index.php';</script>");
?>
9、如何删除数据库中的一条记录
<?php
$sql="delete from liuyan where ly_id=$_GET[id]";
mysql_query($sql,$connec);
echo("<script type='text/javascript'> alert('删除成功!');location.href='../index.php';</script>");
?>
10、如何进行会员登录验证
<?php
session_start();
$username=$_POST["username"];
$password=$_POST["password"];
$sql="select * from admin where username='".$username."' && password='".$password."'";
$result=mysql_query($sql,$connec);
if($row=mysql_fetch_array($result)){
session_register("admin");
$admin=$username;
echo("<script type='text/javascript'> alert('登录成功!');location.href='admin.php';</script>");}
else
{
echo("<script type='text/javascript'> alert('你输入的用户名或密码错误,请重新输入!');location.href='login.php';</script>");
}
mysql_close();
?>
<?php
$connec=mysql_connect("localhost","root","root") or die("不能连接数据库服务器: ".mysql_error());
mysql_select_db("liuyanben",$connec) or die ("不能选择数据库: ".mysql_error());
mysql_query("set names 'gbk'");
?>
2、读取数据库,并实现循环输出
<?php
$sql="select * from liuyan order by ly_id desc";
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){
?>
循环的内容.........
<?php
}
?>
3、如何实现分页,包括两个函数,两个调用
1)两个函数
<?
//分页函数
function genpage(&$sql,$page_size=2)
{
global $prepage,$nextpage,$pages,$sums; //out param
$page = $_GET["page"];
$eachpage = $page_size;
$pagesql = strstr($sql," from ");
$pagesql = "select count(*) as ids ".$pagesql;
$conn = mysql_query($pagesql) or die(mysql_error());
if($rs = mysql_fetch_array($conn)) $sums = $rs[0];
$pages = ceil(($sums-0.5)/$eachpage)-1;
$pages = $pages>=0?$pages:0;
$prepage = ($page>0)?$page-1:0;
$nextpage = ($page<$pages)?$page+1pages;
$startpos = $page*$eachpage;
$sql .=" limit $startpos,$eachpage ";
}
//显示分页
function showpage()
{
global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
$shownum =10/2;
$startpage = ($page>=$shownum)?$page-$shownum:0;
$endpage = ($page+$shownum<=$pages)?$page+$shownumpages;
echo "共".($pages+1)."页: ";
if($page>0)echo "<a href=$PHP_SELF?page=0$queryString>首页</a>";
if($startpage>0)
echo " ... <b><a href=$PHP_SELF?page=".($page-$shownum*2)."$queryString>?</a></b>";
for($i=$startpage;$i<=$endpage;$i++)
{
if($i==$page) echo " <b>[".($i+1)."]</b> ";
else echo " <a href=$PHP_SELF?page=$i$queryString>".($i+1)."</a> ";
}
if($endpage<$pages)
echo "<b><a href=$PHP_SELF?page=".($page+$shownum*2)."$queryString>?</a></b> ... ";
if($page<$pages)
echo "<a href=$PHP_SELF?page=$pages$queryString>尾页</a>";
}
//显示带分类的分页
function showpage1()
{
$fenlei=$_GET["fenleiid"];
global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
$shownum =10/2;
$startpage = ($page>=$shownum)?$page-$shownum:0;
$endpage = ($page+$shownum<=$pages)?$page+$shownumpages;
echo "共".($pages+1)."页: ";
if($page>0)echo "<a href=$PHP_SELF?fenleiid=$fenlei&page=0$queryString>首页</a>";
if($startpage>0)
echo " ... <b><a href=$PHP_SELF?fenleiid=$fenlei&page=".($page-$shownum*2)."$queryString>?</a></b>";
for($i=$startpage;$i<=$endpage;$i++)
{
if($i==$page) echo " <b>[".($i+1)."]</b> ";
else echo " <a href=$PHP_SELF?fenleiid=$fenlei&page=$i$queryString>".($i+1)."</a> ";
}
if($endpage<$pages)
echo "<b><a href=$PHP_SELF?fenleiid=$fenlei&page=".($page+$shownum*2)."$queryString>?</a></b> ... ";
if($page<$pages)
echo "<a href=$PHP_SELF?fenleiid=$fenlei&page=$pages$queryString>尾页</a>";
}
?>
2)两个调用
第一个
<?php
$sql="select * from liuyan order by ly_id desc";
genpage($sql); //只需要正常代码加上这一行就ok。
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){
?>
第二个
<?php
}
?>
<?php
showpage(); //显示页
?>
<?php
mysql_close();
?>
4、服务器端包含
<?php require_once('conn.php'); ?>
5、如何将一条记录写入数据库,然后提示并跳转页面
<?php
$ly_title=$_POST["ly_title"];
$ly_content=$_POST["ly_content"];
$ly_time=$_POST["ly_time"];
$ly_author=$_POST["ly_author"];
$ly_email=$_POST["ly_email"];
$sql="insert into liuyan(ly_title,ly_content,ly_time,ly_author,ly_email) values('".$ly_title."','".$ly_content."','".$ly_time."','".$ly_author."','".$ly_email."')";
mysql_query($sql,$connec);
echo("<script type='text/javascript'> alert('添加成功!');location.href='index.php';</script>");
?>
6、弹出对话框,并发生页面跳转
<?php
echo("<script type='text/javascript'> alert('添加成功!');location.href='index.php';</script>");
?>
7、信息查看页面(有条件读取数据库)
1)有条件读取数据库
<?php
$sql="select * from liuyan where ly_id=$_GET[id]";
$conn=mysql_query($sql,$connec);
$rs=mysql_fetch_array($conn);
?>
2)将某个字段输出
<?=$rs[ly_title]?>
3)关闭数据库
<?php
mysql_close();
?>
8、对数据库中某一条记录进行更新操作,并作提示跳转
<?php
$ly_title=$_POST["ly_title"];
$ly_content=$_POST["ly_content"];
$ly_time=$_POST["ly_time"];
$ly_author=$_POST["ly_author"];
$ly_email=$_POST["ly_email"];
$sql="update liuyan set ly_title='$ly_title',ly_content='$ly_content',ly_time='$ly_time',ly_author='$ly_author',ly_email='$ly_email' where ly_id=$_GET[id]";
mysql_query($sql,$connec);
echo("<script type='text/javascript'> alert('更新成功!');location.href='../index.php';</script>");
?>
9、如何删除数据库中的一条记录
<?php
$sql="delete from liuyan where ly_id=$_GET[id]";
mysql_query($sql,$connec);
echo("<script type='text/javascript'> alert('删除成功!');location.href='../index.php';</script>");
?>
10、如何进行会员登录验证
<?php
session_start();
$username=$_POST["username"];
$password=$_POST["password"];
$sql="select * from admin where username='".$username."' && password='".$password."'";
$result=mysql_query($sql,$connec);
if($row=mysql_fetch_array($result)){
session_register("admin");
$admin=$username;
echo("<script type='text/javascript'> alert('登录成功!');location.href='admin.php';</script>");}
else
{
echo("<script type='text/javascript'> alert('你输入的用户名或密码错误,请重新输入!');location.href='login.php';</script>");
}
mysql_close();
?>