mysql.php obtains the records in the database, a complete summary of personal experience, for reference only!
<?php
/**
*PHP+MYSQL database basic functions
*http://blog.csdn.net/yown
*/
###########################################
#Get sequence ID
###########################################
function getSequence() {
$sql = "update sequence set id=last_insert_id(id+1);";
$sql2= "select last_insert_id();";
global $dbuser,$dbpass,$host,$database,$printsql;
$link = mysql_connect($host,$dbuser,$dbpass);
if(! $link){
return mysql_error();
}
mysql_select_db($database);
mysql_query("SET NAMES UTF8");
if($printsql) echo "<hr/>".$sql."<hr/>";
mysql_query($sql);
if($printsql) echo "<hr/>".$sql2."<hr/>";
$result = mysql_query($sql2);
if(mysql_num_rows($result)==0){
mysql_close($link);
return "";
}
$myrow = mysql_fetch_row($result);
$ret=$myrow[0];
mysql_close($link);
return $ret;
}
###########################################
#Get the Nth column data in the Nth record of strSql, the subscript starts from 1
###########################################
function getData($strsql,$row,$col) {
global $dbuser,$dbpass,$host,$database,$printsql;
$link = mysql_connect($host,$dbuser,$dbpass);
if(! $link){
return mysql_error();
}
mysql_select_db($database);
mysql_query("SET NAMES UTF8");
if($printsql) echo "<hr/>".$strsql."<hr/>";
$result = mysql_query($strsql);
if(mysql_num_rows($result)==0){
mysql_close($link);
return "";
}
$i=0;
while($myrow = mysql_fetch_row($result)){
if($i==$row-1){
$ret=$myrow[$col-1];
break;
}
$i=$i+1;
}
mysql_close($link);
return $ret;
}
##########################################
#Get the Nth record of strSql
###########################################
function getRowData($strsql,$row) {
global $dbuser,$dbpass,$host,$database,$printsql;
$link = mysql_connect($host,$dbuser,$dbpass);
if(! $link){
return mysql_error();
}
mysql_select_db($database);
mysql_query("SET NAMES UTF8");
if($printsql) echo "<hr/>".$strsql."<hr/>";
$result = mysql_query($strsql);
if(mysql_num_rows($result)==0){
mysql_close($link);
return "";
}
$i=0;
while($myrow = mysql_fetch_array($result)){
if($i==$row-1){
$ret=$myrow;
break;
}
$i=$i+1;
}
mysql_close($link);
return $ret;
}
##########################################
#Get the strSql record set and store it in the array
###########################################
function getResultSetData($strsql) {
global $dbuser,$dbpass,$host,$database,$printsql;
$link = mysql_connect($host,$dbuser,$dbpass);
if(! $link){
return mysql_error();
}
mysql_select_db($database);
mysql_query("SET NAMES UTF8");
if($printsql) echo "<hr/>".$strsql."<hr/>";
$result = mysql_query($strsql);
if(mysql_num_rows($result)==0){
mysql_close($link);
return "";
}
while($myrow = mysql_fetch_array($result)){
$ret[]=$myrow;
}
mysql_close($link);
return $ret;
}
##########################################
#Execute strSql
###########################################
function executeSql($strsql) {
global $dbuser,$dbpass,$host,$database,$printsql;
$link = mysql_connect($host,$dbuser,$dbpass);
if(! $link){
return mysql_error();
}
mysql_select_db($database);
mysql_query("SET NAMES UTF8");
if($printsql) echo "<hr/>".$strsql."<hr/>";
mysql_query($strsql);
$ret =mysql_affected_rows($link);
mysql_close($link);
return $ret;
}
/*
Pagination
*/
function Pager(&$curpage,&$pagesize,&$tsql,&$psql,&$totalpage,&$totalrow,&$pagerset){
$curpage=isset($curpage)?intval($curpage):1;/ /current page
$totalpage=0;//Total number of pages
$totalrow=0;//Total number of records
if($printsql) echo "<hr/>".$tsql."<hr/>";
if($curpage <= 0){
$curpage=1;
}
$totalrow=getData($tsql,1,1);//Get the total number of records
$totalrow=strlen(totalrow)==0?0:$totalrow;
if($totalrow>0){
$totalpage=$totalrow%$pagesize==0?(int)($totalrow/$pagesize):(int)($totalrow/$pagesize)+1;
if($curpage>$totalpage){
$curpage=1;
}
$psql=$psql." limit ".(($curpage-1)*$pagesize).",".$pagesize;
if($printsql) echo "<hr/>".$psql."<hr/>";
$pagerset=getResultSetData($psql);//Get the current page record
}
if($totalrow==0||$totalrow=="0"){ $curpage=1;}
}
?>
http://blog.csdn.net/yown/archive/2007/05/15/1610025.aspx