2. Read the database and implement loop output 2. Read the database and implement loop output
Home > Article > Backend Development > Minecraft mobile version seed code encyclopedia PHP common code encyclopedia is a must for beginners
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 implement loop output
$sql="select * from liuyan order by ly_id desc ";
$c
while($rs=mysql_fetch_array($conn)){
?>
The content of the loop...
}
?>
3. How Implement paging, including two functions and two calls
1) Two functions
//Page 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 ";
}
// Display 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)."Page: ";
if($page>0)echo "Homepage";
if($startpage>0)
echo " ... ?";
for($i=$startpage;$i< ;=$endpage;$i++)
{
if($i==$page) echo " [".($i+1)."] ";
else echo " < ;a href=$PHP_SELF?page=$i$queryString>".($i+1)." ";
}
if($endpage<$pages)
echo "< ;a href=$PHP_SELF?page=".($page+$shownum*2)."$queryString>? ... ";
if($page<$pages)
echo "Last page";
}
//Show classified paging
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 "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 calls
The first
$sql="select * from liuyan order by ly_id desc";
genpage($sql); //Just add this line to the normal code and it will be ok.
$c
while($rs=mysql_fetch_array($conn)){
?>
Second
}
?>
showpage(); //Show page
?>
mysql_close();
?>
4. 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_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. A dialog box pops up and the page jumps.
echo("");
? >
7. Information viewing page (conditional reading of the database)
1) Conditional reading of the database
$sql="select * from liuyan where ly_id=$_GET[id]";
$c
$rs=mysql_fetch_array($conn);
?>
2) Output a certain field
=$rs[ly_title]?>
3) Close the database
mysql_close();
?>
8. Update a certain record in the database and make a prompt 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 the database
$sql="delete from liuyan where ly_id=$_GET[id]";
mysql_query($sql,$connec);
echo("");
?>
10. How to verify member login
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. In PHP How to call the editor in
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.
id=content in which content is the name of the hidden field above
14. Loop output (can achieve column splitting)
1) First insert a row and a column of tables
$i=1;
?>
< table> ;?php
if ($i % 2==0) {
echo "