1. Connect MySQL Database code
$c or Die ("Cannot connect to 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 realize the loop output
$sql = "SELECT * from Liuyan ORDER by ly_id Desc";
$c
while ($rs =mysql_fetch_array ($conn)) {
?>
The contents of the loop ...
}
?>
3, how to achieve paging, including two functions, two calls
1) Two functions
Paging functions
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 pagination
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). " Page: ";
if ($page >0) echo "Home";
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";
}
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). " Page: ";
if ($page >0) echo "Home";
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}
?>
2) Two calls
First one
$sql = "SELECT * from Liuyan ORDER by ly_id Desc";
Genpage ($sql); Just the normal code plus this line will be OK.
$c
while ($rs =mysql_fetch_array ($conn)) {
?>
A second
}
?>
ShowPage (); Show Page
?>
Mysql_close ();
?>
4. Server side contains
5, how to write a record to the database, and then prompt and jump page
$ly _title=$_post["Ly_title"];
$ly _c
$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, Pop-up dialog box, and page jump occurred
Echo ("");
?>
7. Information viewing page (conditionally read database)
1) Conditional Read database
$sql = "SELECT * from Liuyan where Ly_id=$_get[id]";
$c
$rs =mysql_fetch_array ($conn);
?>
2) Output a field
3) Close the database
Mysql_close ();
?>
8, the database in a record to update operations, and to make a prompt to jump
$ly _title=$_post["Ly_title"];
$ly _c
$ly _time=$_post["Ly_time"];
$ly _author=$_post["Ly_author"];
$ly _email=$_post["Ly_email"];
$sql = "Update Liuyan set ly_title= ' $ly _title ', Ly_c where Ly_id=$_get[id]";
mysql_query ($sql, $connec);
Echo ("");
?>
9. How to delete a record in a database
$sql = "Delete from Liuyan where Ly_id=$_get[id]";
mysql_query ($sql, $connec);
Echo ("");
?>
10, how to perform member login 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, the method of invoking the editor in PHP
1) Place the Editor folder in the Background Management folder.
2) Use the following statement to introduce the operation.
Note: The name of the EWebEditorPHP38 Editor folder.
Content in Id=content is the name of the hidden field above
14, the loop output (can achieve the breakdown)
1) First insert a row of tables
$i = 1;
?>
while ($rs =mysql_fetch_array ($conn)) {?>
Other tables and outputs that are recycled
| if ($i% 2==0) {echo "
"; } $i + +; }?>
15. Bind data to drop-down list box (and select by default when modified)
$sql = "SELECT * from Fenleibiao"; $c while ($rs 1=mysql_fetch_array ($conn)) {?>" if ($rs [" Fenleiid "]== $rs 1[" Fenleiid "]) {echo" selected ";}? >> }?>
16. Get the character length function
Strlen ($c) >12
17. Define a character intercept 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. Judging whether it is a number
!is_numeric (QQ)
19, PHP technology to get the current date
$ptime =date ("y-m-d");
20. The PHP verification program used by the user when registering
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! = $password 1) {
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, the output of the content to judge, so as to output other results
if ($rs ["active"]==1) {
echo "Activate";
}else{
echo "Disabled";
}
?>
25. Character Intercept function
26. Men's and women's issues or singles with selected
> Male
> Women
27. Single-Selection without a radio box
"> Lock
Else{?>
&action=no "> Unlocking
Its save page is
$hy _id=$_get[' id '];
$action =$_get[' action '];
if ($action = = ' yes ') {
$sql = "Update Hybiao set hy_zhuangtai= ' lock ' where hy_id= ' $id '";
$query =mysql_query ($sql, $connec);
Echo ("");
}
else{
$sql = "Update Hybiao set hy_zhuangtai= ' normal ' where hy_id= ' $id '";
$query =mysql_query ($sql, $connec);
Echo ("");
}
Mysql_close ();
?>
28. If the text is too long, the too long part becomes an ellipsis display
For example, there is a line of text, very long, the table is not displayed in a row.
29.
Disable copying, drag the mouse to select
30. Changes in large and small text
text that requires a specified size
Big Small
30. Add to Favorites and set homepage
32. Festival Countdown
33. Open the window to maximize
34. Join the background music
Only applicable to IE
For Netscape, IE is applicable
35. Scrolling
Scrolling information
36. Prevent clicking on the empty link, the page is often reset to the top end of the page
Code "javascript:void (NULL)" Instead of the original "#" tag
37. Can not click right button, no CTRL + A, can not copy!
39. How to turn off layers
43. Back & Close Window
Back: Javascript:history.back (1)
Close: Javascript:window.close ();
44. If the text is too long, the too long part becomes an ellipsis display
For example, there is a line of text, very long, the table is not displayed in a row.
45. Disable copying, drag the mouse to select
The above is the introduction of my World mobile version of the Seed Code encyclopedia PHP commonly used code to get started, including my world's mobile version of the seed Code Daquan content, I hope that the PHP tutorial interested in a friend helpful.