Home  >  Article  >  Backend Development  >  Comprehensive list of commonly used PHP codes (essential for beginners)_PHP tutorial

Comprehensive list of commonly used PHP codes (essential for beginners)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:36:261006browse

1. Connect the MYSQL database code
$connec=mysql_connect("localhost","root","root") or die("Cannot connect to the database server: ".mysql_error());
mysql_select_db("liuyanben",$connec) or die ("Cannot select database: ".mysql_error());
mysql_query("set names 'gbk'");
?>

2. Read the database and implement loop output
$sql="select * from liuyan order by ly_id desc";
$conn=mysql_query($sql,$connec) ;
while($rs=mysql_fetch_array($conn)){
?>
The content of the loop...
}
?>

3. How to implement paging, including two functions and two calls
1) Two functions
//Paging function
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 ";
}
/ / Show paging
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 "Total".($pages+1)."Pages: ";
if($page>0)echo "Homepage";
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 "Last page";
}
//Show pagination with categories
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 " Total ".($pages+1)." pages: ";
if($page>0)echo "Homepage ";
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 "Last page}
?>
2) Two Call
First
$sql="select * from liuyan order by ly_id desc";
genpage($sql); //Just add this line to the normal code It’s OK.
$conn=mysql_query($sql,$connec);
while($rs=mysql_fetch_array($conn)){
?>
Second
}
?>
showpage(); //Show page
?>
mysql_close();
?>

4. The server side includes


5. How to write a record into the database, and then Prompt and jump to the page
$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. A dialog box pops up and a page jump occurs
echo("");
?>

7. Information viewing Page (conditionally read database)
1) Conditionally read database
$sql="select * from liuyan where ly_id=$_GET[id]";
$ conn=mysql_query($sql,$connec);
$rs=mysql_fetch_array($conn);
?>
2) Output a field

3) Close the database
mysql_close();
?>

8. Update a record in the database and Make prompt jump
$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. How to delete a record in the database
< ?php
$sql="delete from liuyan where ly_id=$_GET[id]";
mysql_query($sql,$connec);
echo("");
?>

10. How to log in as a member Verification
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("

13. How to call the editor in PHP
1) Place the editor folder in the background management folder.
2) Use the following statements to perform the import operation.


Note: The name of the eWebEditorPHP38 editor folder.
The content in id=content is the name of the hidden field above

14. Loop output (can achieve column splitting)
1) First insert a row and a column table
$i=1;
?>



while($rs=mysql_fetch_array($conn)){
?>

if ($i % 2==0) {
echo "
";
}
$i++;
}
?>



15. Bind data to the drop-down list box (and select it by default when modifying)


16. Get character length function
strlen($c)>12

17. Definition A character interception function
Usage:
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. Determine whether it is a number
!is_numeric (qq)

19. Obtaining the current date in PHP technology
$ptime=date("y-m-d");

20. PHP verification program used when user registration
if ($admin=="" or (strlen($admin)>16) or (strlen($admin)<2)) {
echo "";
}
if ( $password=="" or strlen($password)>16 or strlen($password)<6) {
echo "";
}
if ($password=="") {
echo " ";
}else{
if ($password!=$password1) {
echo "";
}
}
if ($wt="") {
echo "< ;SCRIPT language=JavaScript>alert('Password question cannot be empty');";
echo "this.location.href='vbscript:history.back()';";
}
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.字符截取函数


26.男女问题或单选带选择的
>男
>女

27.单选不带单选框的

锁定
else{?>
解锁

它的 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.加入背景音乐
只适用于IE
对Netscape ,IE 都适用

35.滚动

滚动信息


36.防止点击空链接时,页面往往重置到页首端
代码“javascript:void(null)”代替原来的“#”标记

37.不能点右键,不用CTRL+A,不能复制作!
onkeypress="window.event.returnValue=false"
onkeydown="window.event.returnValue=false"
onkeyup="window.event.returnValue=false"
ondragstart="window.event.returnValue=false"
onselectstart="event.returnValue=false">


37.随机变换背景图象(一个可以刷新心情的特效)


38.划过链接 手型鼠标
style="cursor:hand"

39.如何关闭层

关闭层

40.[关闭窗口]

41.凹陷文字背景为灰色


怎么样,我凹下去了吧?

你不想试试吗?

www.lenvo.cn



42.给表格做链接

Other tables and outputs to be looped






43.后退&关闭窗口
后退:javascript:history.back(1)
关闭:javascript:window.close();

44.如果文字过长,则将过长的部分变成省略号显示

就是比如有一行文字,很长,表格内一行显示不下.


45. Copying is prohibited, drag the mouse to select

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/322138.htmlTechArticle1. Code to connect to MYSQL database?php $connec=mysql_connect("localhost","root","root" ) or die("Unable to connect to the database server: ".mysql_error()); mysql_select_db("liuyanben",$connec)...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn