PHP常用代码大全
1、连接MYSQL数据库代码
$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、读取数据库,并实现循环输出
$sql="select * from liuyan order by ly_id desc";
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){
?>
循环的内容.........
}
?>
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 $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
echo "共".($pages+1)."页: ";
if($page>0)echo "首页";
if($startpage>0)
echo " ... ?";
for($i=$startpage;$i {
if($i==$page) echo " [".($i+1)."] ";
else echo " ".($i+1)." ";
}
if($endpage echo "? ... ";
if($page 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
echo "共".($pages+1)."页: ";
if($page>0)echo "首页";
if($startpage>0)
echo " ... ?";
for($i=$startpage;$i {
if($i==$page) echo " [".($i+1)."] ";
else echo " ".($i+1)." ";
}
if($endpage echo "? ... ";
if($page echo "尾页& amp; gt;";
}
?>
2)两个调用
第一个
$sql="select * from liuyan order by ly_id desc";
genpage($sql); //只需要正常代码加上这一行就ok。
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){
?>
第二个
}
?>
showpage(); //显示页
?>
mysql_close();
?>
4、服务器端包含
5、如何将一条记录写入数据库,然后提示并跳转页面
$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("");
?>
6、 弹出对话框,并发生页面跳转
echo("");
?>
7、 信息查看页面(有条件读取数据库)
1)有条件读取数据库
$sql="select * from liuyan where ly_id=$_GET[id]";
$conn=mysql_query($sql,$connec);
$rs=mysql_fetch_array($conn);
?>
2) 将某个字段输出
=$rs[ly_title]?>
3)关闭数据库
mysql_close();
?>
8、对数据库中某一条记录进行更新操作,并作提示跳转
$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("");
?>
9、 如何删除数据库中的一条记录
$sql="delete from liuyan where ly_id=$_GET[id]";
mysql_query($sql,$connec);
echo("");
?>
10、 如何进行会员登录验证
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("");}
else
{
echo("& gt;");
}
mysql_close();
?>
11、如何对SESSION进行检验(后台检查页面的制作)
session_start();
if(!isset($_SESSION["admin"])){
header("location:login.php");
exit;
}
?>
12、 验证用户名及密码是否填写(javascript)
13、 在PHP中调用编辑器的方法
1)将编辑器文件夹放置后台管理文件夹内。
2)利用以下语句进行引入操作。
注:eWebEditorPHP38编辑器文件夹的名称。
id=content中content为上面隐藏域的名称
14、循环输出(能够实现分列)
1)首先插入一行一列表格
$i=1;
?>
被循环的其它表格和输出 |
15、 给下拉列表框绑定数据(并且在修改时默认选中)
16、获取字符长度函数
strlen($c)>12
17、 定义一个字符截取函数
用法:=substrgb($rs["title"],10)?>
function substrgb($in,$num){
$pos=0;
$out="";
while($c=substr($in,$pos,1)){
if($c=="\n") 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、判断是否是数字
!is_numeric(qq)
19、PHP技术中获取当前日期
$ptime=date("y-m-d");
20、用户注册时所使用的PHP验证程序
if ($admin=="" or (strlen($admin)>16) or (strlen($admin) echo "";
}
if ($password=="" or strlen($password)>16 or strlen($password) echo "";
}
if ($password=="") {
echo "";
}else{
if ($password!=$password1) {
echo "";
}
}
if ($wt="") {
echo "";
}
if ($da="") {
echo "";
}
if ($qq!="") {
if (!is_numeric($qq)) {
echo "";
}
}
if ($youbian=="" or strlen($youbian)!=6) {
echo "";
}
if ($youbian!="") {
if (!is_numeric($youbian)) {
echo "";
}
}
if ($dizhi="") {
echo "";
}
if ($mail=="") {
echo "";
}
if ($textarea=="") {
echo "";
}
if ($textarea=="" or strlen(textarea)>150) {
echo "";
}
24、对输出的内容进行判断,从而输出其它结果
if ($rs["active"]==1) {
echo "激活";
}else{
echo "禁用";
}
?>
25.字符截取函数
=substr("$rs[zixun_biaoti]",0,28)?>
26.男女问题或单选带选择的
>男
>女
27.单选不带单选框的
">锁定
else{?>
&action=no">解锁
它的 save页是
$hy_id=$_GET['id'];
$action=$_GET['action'];
if ($action=='yes'){
$sql="update hybiao set hy_zhuangtai='锁定' where hy_id='$id'";
$query=mysql_query($sql,$connec);
echo("");
}
else{
$sql="update hybiao set hy_zhuangtai='正常' where hy_id='$id'";
$query=mysql_query($sql,$connec);
echo("");
}
mysql_close();
?>
28. 如果文字过长,则将过长的部分变成省略号显示
29.
禁止复制,鼠标拖动选取
30.大 中 小 文字的变化
需要指定大小的文字
大 中 小
30.添加到收藏夹和设为首页
设为首页
收藏本站
31.记录并显示网页的最后修改时间
32.节日倒计时
33.打开窗口即最大化
34.加入背景音乐
<script></script><script></script><script></script>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.
