PHP原始為Personal Home Page的縮寫,已經(jīng)正式更名為 "PHP: Hypertext Preprocessor"。本文特意為大家收集整理了PHP常用代碼大全,希望大家喜歡!
1、連接MYSQL數(shù)據(jù)庫代碼
<?php
$connec=mysql_connect("localhost","root","root") or die("不能連接數(shù)據(jù)庫服務(wù)器: ".mysql_error());
mysql_select_db("liuyanben",$connec) or die ("不能選擇數(shù)據(jù)庫: ".mysql_error());
mysql_query("set names 'gbk'");
?>
2、讀取數(shù)據(jù)庫,并實(shí)現(xiàn)循環(huán)輸出
<?php
$sql="select * from liuyan order by ly_id desc";
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){
?>
循環(huán)的內(nèi)容.........
<?php
}
?>
3、如何實(shí)現(xiàn)分頁,包括兩個(gè)函數(shù),兩個(gè)調(diào)用
1)兩個(gè)函數(shù)
<?
//分頁函數(shù)
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+1:$pages;
$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+$shownum:$pages;
echo "共".($pages+1)."頁: ";
if($page>0)echo "首頁";
if($startpage>0)
echo " ... ?";
for($i=$startpage;$i<=$endpage;$i++)
{
if($i==$page) echo " [".($i+1)."] ";
else echo " ".($i+1)." ";
}
if($endpage<$pages)
echo "? ... ";
if($page<$pages)
echo "尾頁";
}
//顯示帶分類的分頁
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+$shownum:$pages;
echo "共".($pages+1)."頁: ";
if($page>0)echo "首頁";
if($startpage>0)
echo " ... ?";
for($i=$startpage;$i<=$endpage;$i++)
{
if($i==$page) echo " [".($i+1)."] ";
else echo " ".($i+1)." ";
}
if($endpage<$pages)
echo "? ... ";
if($page<$pages)
echo "尾頁
}
?>
2)兩個(gè)調(diào)用
第一個(gè)
<?php
$sql="select * from liuyan order by ly_id desc";
genpage($sql); //只需要正常代碼加上這一行就ok。
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){
?>
第二個(gè)
<?php
}
?>
<?php
showpage(); //顯示頁
?>
<?php
mysql_close();
?>
4、服務(wù)器端包含
<?php require_once('conn.php'); ?>
5、如何將一條記錄寫入數(shù)據(jù)庫,然后提示并跳轉(zhuǎn)頁面
<?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=" 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、 彈出對話框,并發(fā)生頁面跳轉(zhuǎn)
<?php
echo("<script type='text/javascript'> alert('添加成功!');location.href='index.php';</script>");
?>
7、 信息查看頁面(有條件讀取數(shù)據(jù)庫)
1)有條件讀取數(shù)據(jù)庫
<?php
$sql="select * from liuyan where ly_id=$_GET[id]";
$conn=mysql_query($sql,$connec);
$rs=mysql_fetch_array($conn);
?>
2) 將某個(gè)字段輸出
<?=$rs[ly_title]?>
3)關(guān)閉數(shù)據(jù)庫
<?php
mysql_close();
?>
8、對數(shù)據(jù)庫中某一條記錄進(jìn)行更新操作,并作提示跳轉(zhuǎn)
<?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="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、 如何刪除數(shù)據(jù)庫中的一條記錄
<?php
$sql=" from liuyan where ly_id=$_GET[id]";
mysql_query($sql,$connec);
echo("<script type='text/javascript'> alert('刪除成功!');location.href='../index.php';</script>");
?>
10、 如何進(jìn)行會員登錄驗(yàn)證
<?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('你輸入的用戶名或密碼錯(cuò)誤,請重新輸入!');location.href='login.php';
}
mysql_close();
?>
11、如何對SESSION進(jìn)行檢驗(yàn)(后臺檢查頁面的制作)
<?php
session_start();
if(!isset($_SESSION["admin"])){
header("location:login.php");
exit;
}
?>
12、 驗(yàn)證用戶名及密碼是否填寫(javascript)
<script language=javascript>
</script>
13、 在PHP中調(diào)用編輯器的方法
1)將編輯器文件夾放置后臺管理文件夾內(nèi)。
2)利用以下語句進(jìn)行引入操作。
注:eWebEditorPHP38編輯器文件夾的名稱。
id=content中content為上面隱藏域的名稱
14、循環(huán)輸出(能夠?qū)崿F(xiàn)分列)
1)首先插入一行一列表格
<?php
$i=1;
?>
被循環(huán)的其它表格和輸出
|
15、 給下拉列表框綁定數(shù)據(jù)(并且在修改時(shí)默認(rèn)選中)
<?php
$sql="select * from fenleibiao";
$conn=mysql_query($sql,$connec);
while($rs1=mysql_fetch_array($conn)){
?>
<?
if ($rs["fenleiid"]==$rs1["fenleiid"]){
echo "selected" ;
}
?>>
<?=$rs1["flname"]?>
<?php>
}
?>
16、獲取字符長度函數(shù)
strlen($c)>12
17、 定義一個(gè)字符截取函數(shù)
用法:<?=substrgb($rs["title"],10)?>
function substrgb($in,$num){
$pos=0;
$out="";
while($c=substr($in,$pos,1)){
if($c=="") break;
if(ord($c)>128){
$out.=$c;
$pos++;
$c=substr($in,$pos,1);
$out.=$c;
}else{
$out.=$c;
}
$pos++;
if($pos>=$num) break;
}
if($out!=$in) $out = $out . "...";
return $out;
}
18、判斷是否是數(shù)字
!is_numeric(qq)
19、PHP技術(shù)中獲取當(dāng)前日期
$ptime=date("y-m-d");
20、用戶注冊時(shí)所使用的PHP驗(yàn)證程序
if ($admin=="" or (strlen($admin)>16) or (strlen($admin)<2)) {
echo "<script language=JavaScript>alert('請輸入用戶名(不能大于16小于2)');";
echo"this.location.href='vbscript:history.back()';</script>";
}
if ($password=="" or strlen($password)>16 or strlen($password)<6) {
echo "<script language=JavaScript>alert('密碼長度為6-16個(gè)字符');";
echo"this.location.href='vbscript:history.back()';</script>";
}
if ($password=="") {
echo "<script language=JavaScript>alert('確認(rèn)密碼不能為空');";
echo"this.location.href='vbscript:history.back()';</script>";
}else{
if ($password!=$password1) {
echo "<script language=JavaScript>alert('密碼和確認(rèn)密碼不一致');";
echo"this.location.href='vbscript:history.back()';</script>";
}
}
if ($wt="") {
echo "<script language=JavaScript>alert('密碼問題不能為空');";
echo"this.location.href='vbscript:history.back()';</script>";
}
if ($da="") {
echo "<script language=JavaScript>alert('問題答案不能為空');";
echo"this.location.href='vbscript:history.back()';</script>";
}
if ($qq!="") {
if (!is_numeric($qq)) {
echo "<script language=JavaScript>alert('QQ號碼必須是數(shù)字');";
echo"this.location.href='vbscript:history.back()';</script>";
}
}
if ($youbian=="" or strlen($youbian)!=6) {
echo "<script language=JavaScript>alert('請正確輸入郵編');";
echo"this.location.href='vbscript:history.back()';</script>";
}
if ($youbian!="") {
if (!is_numeric($youbian)) {
echo "<script language=JavaScript>alert('郵編必須是數(shù)字');";
echo"this.location.href='vbscript:history.back()';</script>";
}
}
if ($d i z h i="") {
echo "<script language=JavaScript>alert('住址不能為空');";
echo"this.location.href='vbscript:history.back()';</script>";
}
if ($mail=="") {
echo "<script language=JavaScript>alert('E-mail不能為空!');";
echo "this.location.href='vbscript:history.back()';</script>";
}
if ($textarea=="") {
echo "<script language=JavaScript>alert('個(gè)人說明不能為空!');";
echo "this.location.href='vbscript:history.back()';</script>";
}
if ($textarea=="" or strlen(textarea)>150) {
echo "<script language=JavaScript>alert('個(gè)人說明為150個(gè)字符');";
echo"this.location.href='vbscript:history.back()';</script>";
}
24、對輸出的內(nèi)容進(jìn)行判斷,從而輸出其它結(jié)果
<?php
if ($rs["active"]==1) {
echo "激活";
}else{
echo "禁用";
}
?>
25.字符截取函數(shù)
<?=substr("$rs[zixun_biaoti]",0,28)?>
26.男女問題或單選帶選擇的
>男
>女
27.單選不帶單選框的
<?php if ($rs['hy_zhuangtai']=='鎖定'){?>
<?php }
else{?>
<?php }?>
它的 save頁是
<?php require_once('http://www.cnblogs.com/conn.php'); ?>
<?php
$hy_id=$_GET['id'];
$action=$_GET['action'];
if ($action=='yes'){
$sql="hybiao set hy_zhuangtai='鎖定' where hy_id='$id'";
$query=mysql_query($sql,$connec);
echo("<script type='text/javascript'>location.href='UserManage.php';</script>");
}
else{
$sql="hybiao set hy_zhuangtai='正常' where hy_id='$id'";
$query=mysql_query($sql,$connec);
echo("<script type='text/javascript'>location.href='UserManage.php';</script>");
}
mysql_close();
?>
28. 如果文字過長,則將過長的部分變成省略號顯示