php常用代码学习
PHP常用代码
最近在学习php,看视频的时候记了点。
1、连接MYSQL数据库代码
1<?php
2$connec=mysql_connect("localhost","root","root") or die("不能连接数据库服务器: ".mysql_error());
3mysql_select_db("liuyanben",$connec) or die ("不能选择数据库: ".mysql_error());
4mysql_query("set names 'gbk'");
5?>
2、读取数据库,并实现循环输出
1<?php
2$sql="select * from liuyan order by ly_id desc";
3$conn=mysql_query($sql,$connec);
4while($rs=mysql_fetch_array($conn)){
5?>
6循环的内容.........
7<?php
8}
9?>
3、如何实现分页,包括两个函数,两个调用
1)两个函数
1<?
2//分页函数
3function genpage(&$sql,$page_size=2)
4{
5 global $prepage,$nextpage,$pages,$sums; //out param
6 $page = $_GET["page"];
7 $eachpage = $page_size;
8 $pagesql = strstr($sql," from ");
9 $pagesql = "select count(*) as ids ".$pagesql;
10 $conn = mysql_query($pagesql) or die(mysql_error());
11 if($rs = mysql_fetch_array($conn)) $sums = $rs[0];
12 $pages = ceil(($sums-0.5)/$eachpage)-1;
13 $pages = $pages>=0?$pages:0;
14 $prepage = ($page>0)?$page-1:0;
15 $nextpage = ($page<$pages)?$page+1:$pages;
16 $startpos = $page*$eachpage;
17 $sql .=" limit $startpos,$eachpage ";
18}
19//显示分页
20function showpage()
21{
22 global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
23 $shownum =10/2;
24 $startpage = ($page>=$shownum)?$page-$shownum:0;
25 $endpage = ($page+$shownum<=$pages)?$page+$shownum:$pages;
26
27 echo "共".($pages+1)."页: ";
28 if($page>0)echo "<a href=$PHP_SELF?page=0$queryString>首页</a>";
29 if($startpage>0)
30 echo " ... <b><a href=$PHP_SELF?page=".($page-$shownum*2)."$queryString>?</a></b>";
31 for($i=$startpage;$i<=$endpage;$i++)
32 {
33 if($i==$page) echo " <b>[".($i+1)."]</b> ";
34 else echo " <a href=$PHP_SELF?page=$i$queryString>".($i+1)."</a> ";
35 }
36 if($endpage<$pages)
37 echo "<b><a href=$PHP_SELF?page=".($page+$shownum*2)."$queryString>?</a></b> ... ";
38 if($page<$pages)
39 echo "<a href=$PHP_SELF?page=$pages$queryString>尾页</a>";
40}
41//显示带分类的分页
42function showpage1()
43{
44$fenlei=$_GET["fenleiid"];
45 global $page,$pages,$prepage,$nextpage,$queryString; //param from genpage function
46 $shownum =10/2;
47 $startpage = ($page>=$shownum)?$page-$shownum:0;
48 $endpage = ($page+$shownum<=$pages)?$page+$shownum:$pages;
49
50 echo "共".($pages+1)."页: ";
51 if($page>0)echo "<a href=$PHP_SELF?fenleiid=$fenlei&page=0$queryString>首页</a>";
52 if($startpage>0)
53 echo " ... <b><a href=$PHP_SELF?fenleiid=$fenlei&page=".($page-$shownum*2)."$queryString>?</a></b>";
54 for($i=$startpage;$i<=$endpage;$i++)
55 {
56 if($i==$page) echo " <b>[".($i+1)."]</b> ";
57 else echo " <a href=$PHP_SELF?fenleiid=$fenlei&page=$i$queryString>".($i+1)."</a> ";
58 }
59 if($endpage<$pages)
60 echo "<b><a href=$PHP_SELF?fenleiid=$fenlei&page=".($page+$shownum*2)."$queryString>?</a></b> ... ";
61 if($page<$pages)
62 echo "<a href=$PHP_SELF?fenleiid=$fenlei&page=$pages$queryString>尾页</a>";
63}
64?>
2)两个调用
1第一个
2<?php
3$sql="select * from liuyan order by ly_id desc";
4genpage($sql); //只需要正常代码加上这一行就ok。
5$conn=mysql_query($sql,$connec);
6while($rs=mysql_fetch_array($conn)){
7?>
8第二个
9<?php
10}
11?>
12<?php
13showpage(); //显示页
14?>
15<?php
16mysql_close();
17?>
4、服务器端包含
1<?php require_once('conn.php'); ?>
5、如何将一条记录写入数据库,然后提示并跳转页面
1<?php
2$ly_title=$_POST["ly_title"];
3$ly_content=$_POST["ly_content"];
4$ly_time=$_POST["ly_time"];
5$ly_author=$_POST["ly_author"];
6$ly_email=$_POST["ly_email"];
7$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."')";
8mysql_query($sql,$connec);
9echo("<script type='text/javascript'> alert('添加成功!');location.href='index.php';</script>");
10?>
6、弹出对话框,并发生页面跳转
1<?php
2echo("<script type='text/javascript'> alert('添加成功!');location.href='index.php';</script>");
3?>
7、信息查看页面(有条件读取数据库)
11)有条件读取数据库
2<?php
3$sql="select * from liuyan where ly_id=$_GET[id]";
4$conn=mysql_query($sql,$connec);
5$rs=mysql_fetch_array($conn);
6?>
7
82)将某个字段输出
9<?=$rs[ly_title]?>
10
113)关闭数据库
12<?php
13mysql_close();
14?>
8、对数据库中某一条记录进行更新操作,并作提示跳转
1<?php
2$ly_title=$_POST["ly_title"];
3$ly_content=$_POST["ly_content"];
4$ly_time=$_POST["ly_time"];
5$ly_author=$_POST["ly_author"];
6$ly_email=$_POST["ly_email"];
7$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]";
8mysql_query($sql,$connec);
9echo("<script type='text/javascript'> alert('更新成功!');location.href='../index.php';</script>");
10?>
9、如何删除数据库中的一条记录
1<?php
2$sql="delete from liuyan where ly_id=$_GET[id]";
3mysql_query($sql,$connec);
4echo("<script type='text/javascript'> alert('删除成功!');location.href='../index.php';</script>");
5?>
10、如何进行会员登录验证
1<?php
2session_start();
3$username=$_POST["username"];
4$password=$_POST["password"];
5$sql="select * from admin where username='".$username."' && password='".$password."'";
6$result=mysql_query($sql,$connec);
7if($row=mysql_fetch_array($result)){
8session_register("admin");
9$admin=$username;
10echo("<script type='text/javascript'> alert('登录成功!');location.href='admin.php';</script>");}
11else
12{
13echo("<script type='text/javascript'> alert('你输入的用户名或密码错误,请重新输入!');location.href='login.php';</script>");
14}
15mysql_close();
16?>
11、如何对SESSION进行检验(后台检查页面的制作)
1<?php
2session_start();
3if(!isset($_SESSION["admin"])){
4header("location:login.php");
5exit;
6}
7?>
12、验证用户名及密码是否填写(javascript)
1<SCRIPT language=javascript>
2<!--
3function confirmlogin()
4{
5if (document.frmmain.username.value.length<4 || document.frmmain.username.value=="")
6{
7 document.frmmain.username.focus();
8 document.frmmain.username.select;
9 window.alert("请输入你的用户名!");
10 return false;
11}
12if (document.frmmain.password.value.length<4)
13{
14 document.frmmain.password.focus();
15 document.frmmain.password.select;
16 window.alert("请输入你的密码!");
17 return false;
18}
19 return true;
20}
21//-->
22</SCRIPT>
13、在PHP中调用编辑器的方法
1)将编辑器文件夹放置后台管理文件夹内。
2)利用以下语句进行引入操作。
1<input name="content" type="hidden" value=''>
2<IFRAME ID="eWebEditor1" src="eWebEditorPHP38/ewebeditor.htm?id=content&style=coolblue" frameborder="0" scrolling="no" width="550" height="350"></IFRAME>
注:eWebEditorPHP38编辑器文件夹的名称。
id=content中content为上面隐藏域的名称
14、循环输出(能够实现分列)
1)首先插入一行一列表格
1<?php
2$i=1;
3?>
4<table>
5 <tr>
6 <?php
7while($rs=mysql_fetch_array($conn)){
8?>
9 <td>
10 被循环的其它表格和输出
11 </td>
12 <?php
13 if ($i % 2==0) {
14 echo "</tr><tr>";
15 }
16 $i++;
17 }
18 ?>
19 </tr>
20</table>
15、给下拉列表框绑定数据(并且在修改时默认选中)
1<select name="fenleiid">
2<?php
3$sql="select * from fenleibiao";
4$conn=mysql_query($sql,$connec);
5while($rs1=mysql_fetch_array($conn)){
6?>
7
8<option value="<?=$rs1["fenleiid"]?>"
9<?
10if ($rs["fenleiid"]==$rs1["fenleiid"]){
11echo "selected" ;
12}
13?>>
14<?=$rs1["flname"]?>
15</option>
16 <?php>
17}
18?>
19 </select>
16、获取字符长度函数
1strlen($c)>12
17、定义一个字符截取函数
1用法:<?=substrgb($rs["title"],10)?>
2function substrgb($in,$num){
3 $pos=0;
4 $out="";
5 while($c=substr($in,$pos,1)){
6 if($c=="n") break;
7 if(ord($c)>128){
8 $out.=$c;
9 $pos++;
10 $c=substr($in,$pos,1);
11
12 $out.=$c;
13 }else{
14 $out.=$c;
15 }
16 $pos++;
17 if($pos>=$num) break;
18 }
19 if($out!=$in) $out = $out . "...";
20 return $out;
21}
18、判断是否是数字
1!is_numeric(qq)
19、PHP技术中获取当前日期
1$ptime=date("y-m-d");
20、用户注册时所使用的PHP验证程序
1if ($admin=="" or (strlen($admin)>16) or (strlen($admin)<2)) {
2 echo "<SCRIPT language=JavaScript>alert('请输入用户名(不能大于16小于2)');";
3 echo"this.location.href='vbscript:history.back()';</SCRIPT>";
4}
5if ($password=="" or strlen($password)>16 or strlen($password)<6) {
6 echo "<SCRIPT language=JavaScript>alert('密码长度为6-16个字符');";
7 echo"this.location.href='vbscript:history.back()';</SCRIPT>";
8}
9if ($password=="") {
10 echo "<SCRIPT language=JavaScript>alert('确认密码不能为空');";
11 echo"this.location.href='vbscript:history.back()';</SCRIPT>";
12}else{
13if ($password!=$password1) {
14 echo "<SCRIPT language=JavaScript>alert('密码和确认密码不一致');";
15 echo"this.location.href='vbscript:history.back()';</SCRIPT>";
16}
17}
18if ($wt="") {
19 echo "<SCRIPT language=JavaScript>alert('密码问题不能为空');";
20 echo"this.location.href='vbscript:history.back()';</SCRIPT>";
21}
22if ($da="") {
23 echo "<SCRIPT language=JavaScript>alert('问题答案不能为空');";
24 echo"this.location.href='vbscript:history.back()';</SCRIPT>";
25}
26if ($qq!="") {
27if (!is_numeric($qq)) {
28 echo "<SCRIPT language=JavaScript>alert('QQ号码必须是数字');";
29 echo"this.location.href='vbscript:history.back()';</SCRIPT>";
30}
31}
32if ($youbian=="" or strlen($youbian)!=6) {
33 echo "<SCRIPT language=JavaScript>alert('请正确输入邮编');";
34 echo"this.location.href='vbscript:history.back()';</SCRIPT>";
35}
36if ($youbian!="") {
37if (!is_numeric($youbian)) {
38 echo "<SCRIPT language=JavaScript>alert('邮编必须是数字');";
39 echo"this.location.href='vbscript:history.back()';</SCRIPT>";
40}
41}
42if ($dizhi="") {
43 echo "<SCRIPT language=JavaScript>alert('住址不能为空');";
44 echo"this.location.href='vbscript:history.back()';</SCRIPT>";
45}
46if ($mail=="") {
47 echo "<SCRIPT language=JavaScript>alert('E-mail不能为空!');";
48 echo "this.location.href='vbscript:history.back()';</SCRIPT>";
49}
50if ($textarea=="") {
51 echo "<SCRIPT language=JavaScript>alert('个人说明不能为空!');";
52 echo "this.location.href='vbscript:history.back()';</SCRIPT>";
53}
54if ($textarea=="" or strlen(textarea)>150) {
55 echo "<SCRIPT language=JavaScript>alert('个人说明为150个字符');";
56 echo"this.location.href='vbscript:history.back()';</SCRIPT>";
57}
24、对输出的内容进行判断,从而输出其它结果
1<?php
2 if ($rs["active"]==1) {
3 echo "<font color='#ff0000'>激活</font>";
4 }else{
5 echo "禁用";
6 }
7?>
25.字符截取函数
1<?=substr("$rs[zixun_biaoti]",0,28
2 )?>
26.男女问题或单选带选择的
1<input type="radio" name="hy_zhuangtai" value="男" <?php if ($rs["hy_zhungtai"]==="男") { echo "checked";}?>>
2 男
3<input type="radio" name="hy_zhuangtai" value="女" <?php if ($rs["hy_zhuangtai"]==="女") { echo "checked";}?>>
4 女
27.单选不带单选框的
1<?php if ($rs['hy_zhuangtai']=='锁定'){?>
2 <a href="Userzt.php?action=yes&id=<?php echo $rs['hy_id'];?>">锁定</a>
3 <?php }
4 else{
5 ?><a href="Userzt.php?id=<?php echo $rs['hy_id'];?>&action=no">解锁</a>
6 <?php }?>
7它的 save页是
8<?php require_once('../../conn.php'); ?>
9<?php
10$hy_id=$_GET['id'];
11$action=$_GET['action'];
12if ($action=='yes'){
13$sql="update hybiao set hy_zhuangtai='锁定' where hy_id='$id'";
14$query=mysql_query($sql,$connec);
15echo("<script type='text/javascript'>location.href='UserManage.php';</script>");
16}
17else{
18$sql="update hybiao set hy_zhuangtai='正常' where hy_id='$id'";
19$query=mysql_query($sql,$connec);
20echo("<script type='text/javascript'>location.href='UserManage.php';</script>");
21}
22mysql_close();
23?>
28. 禁止复制,鼠标拖动选取
1<body ondragstart=window.event.returnValue=false oncontextmenu=window.event.returnValue=false onselectstart=event.returnValue=false>
30.大 中 小 文字的变化
1<script type="text/javascript">
2function doZoom(size)
3{document.getElementById('zoom').style.fontSize=size+'px';}
4</script>
5<span id="zoom">需要指定大小的文字</span>
6<a href="javascript:doZoom(16)">大</a> <a href="javascript:doZoom(14)">中</a> <a href="javascript:doZoom(12)">小</a>
30.添加到收藏夹和设为首页
1<a href=# onclick="this.style.behavior='url(#default#homepage)';this.setHomePage('http://www.makewing.com/lanren/');">设为首页</a>
2<a href="javascript:window.external.AddFavorite('http://www.makewing.com/lanren/','懒人图库')">收藏本站</a>
31.记录并显示网页的最后修改时间
1<script language=JavaScript>
2document.write("最后更新时间: " + document.lastModified + "")
3</script>
32.节日倒计时
1<Script Language="JavaScript">
2 var timedate= new Date("October 1,2002");
3 var times= "国庆节";
4 var now = new Date();
5 var date = timedate.getTime() - now.getTime();
6 var time = Math.floor(date / (1000 * 60 * 60 * 24));
7 if (time >= 0)
8 document.write( "现在离"+times+"还有: "+time +"天")
9</Script>
33.打开窗口即最大化
1<script language="JavaScript">
2<!-- Begin
3self.moveTo(0,0)
4self.resizeTo(screen.availWidth,screen.availHeight)
5// End -->
6</script>
34.加入背景音乐
1<bgsound src="mid/windblue[1].mid" loop="-1"> 只适用于IE
2<embed src="music.mid" autostart="true" loop="true" hidden="true"> 对Netscape ,IE 都适用
35.滚动
1<marquee direction=up height=146 onmouseout=start() onmouseover=stop() scrollAmount=2>
2滚动信息
3</marquee>
37.不能点右键,不用CTRL+A,不能复制作!
1<body oncontextmenu="window.event.returnValue=false"
2onkeypress="window.event.returnValue=false"
3onkeydown="window.event.returnValue=false"
4onkeyup="window.event.returnValue=false"
5ondragstart="window.event.returnValue=false"
6onselectstart="event.returnValue=false">
7</body>
37.随机变换背景图象(一个可以刷新心情的特效)
1<Script Language="JavaScript">
2 image = new Array(4); //定义image为图片数量的数组
3 image [0] = 'tu0.gif' //背景图象的路径
4 image [1] = 'tu1.gif'
5 image [2] = 'tu2.gif'
6 image [3] = 'tu3.gif'
7 image [4] = 'tu4.gif'
8 number = Math.floor(Math.random() * image.length);
9 document.write("<BODY BACKGROUND="+image[number]+">");
10</Script>
38.划过链接 手型鼠标
1style="cursor:hand"
39.如何关闭层
1<div id="Layer1"></div>
2<a href="#" onClick="Layer1.style.display='none'">关闭层</a>