我的常用的Mysql_func.php数据库处理函数页面

发布时间:2008年03月04日      浏览次数:1243 次
<?php
//添加信息
//调用说明:she_cus为数据库表名,之后的为要添加的字段名称以及值
//AddInfo("she_cus||cus_id||$id||cus_name||$Cus_Name||cus_phone||$Cus_Phone||cus_phone||$Cus_Phone");
function AddInfo($str){
      $strarray = explode("||",$str);
      $strsum = count($strarray)-1;
      for($i=1;$i<=$strsum;$i++){
            if (substr_count($i/2,'.')>=1){
                  if ($i==$strsum-1){
                        $lan = ' ';
                  }else{
                        $lan = ',';
                  }
                  $sp1 .= $strarray[$i].$lan;
            }else{
                  if ($i==$strsum){
                        $dian = ' ';
                  }else{
                        $dian = ', ';
                  }
                  $sp2 .= '\''.$strarray[$i].'\' '.$dian;
            }
      }
      $sql = "insert into `$strarray[0]` ($sp1) values ($sp2)";
      if (mysql_query($sql)){
            //echo '添加成功<p>';
      }else{
            //echo '系统错误';
            //if ($ErrorMsg==1){
                  //echo die(mysql_error());//显示错误信息
            //}else{
                  //不显示错误信息
            //}
      }
}
//----------------------------------------------------------------------------------------|
//修改信息
//调用说明:
//示例代码如下,she_cus为数据库表名,cus_id为条件判断数据库字段,$id为条件判断值,之后的为要修改的字段名称以及值
//EditInfo("she_cus||cus_id||$id||cus_name||$Cus_Name||cus_phone||$Cus_Phone");
function EditInfo($str){
      $strarray = explode("||",$str);
      $strsum = count($strarray)-1;
      for($i=3;$i<=$strsum;$i++){
            if (substr_count($i/2,'.')>=1){
                  $lan = '=';
                  $sp .= $strarray[$i].$lan;
            }else{
                  if ($i==$strsum){
                        $dian = ' ';
                  }else{
                        $dian = ', ';
                  }
                  $sp .= '\''.$strarray[$i].'\' '.$dian;
            }
      }
      $sql = "update `$strarray[0]` set $sp where $strarray[1]='$strarray[2]'";
      if (mysql_query($sql)){
            //echo '保存成功<p>';
      }else{
            echo '系统错误';
            //if ($ErrorMsg==1){
                  echo die(mysql_error());//显示错误信息
            //}else{
                  //不显示错误信息
            //}
      }
}
//----------------------------------------------------------------------------------------|
//删除信息,$tablename为数据库表名,$idname为条件数据库字段名,$id为条件值
function DelInfo($tablename,$idname,$id){
      $sql= "delete from $tablename where $idname='$id'";
      mysql_query($sql) or die(mysql_error());
}
//----------------------------------------------------------------------------------------|
//分页函数
function genpage(&$sql,$page_size=10)
{
global $page,$prepage,$nextpage,$pages,$sums; //out param
$eachpage = $page_size;
       if ($eachpage<0)$eachpage=10;
$pagesql = strstr($sql," from ");
$pagesql = "select count(*) as ids ".$pagesql;
$result = mysql_query($pagesql) or die(mysql_error());
if($rss = mysql_fetch_array($result)) $sums = $rss[0];
$pages = ceil(($sums-0.5)/$eachpage);
$pages = $pages>0?$pages:0;
       $page=1;
       if (is_numeric(trim($_GET["page"])) ){$page=trim($_GET["page"]);}
       if ($page<1 || $page=="" ) {$page=1;}
       if ($page>$pages ) {$page=$pages;}
$prepage = ($page>1)?$page-1:1;
$nextpage = ($page<$pages)?$page+1:$pages;
$startpos = ($page-1)*$eachpage;
       if ($startpos<0)$startpos=0;
$sql .=" limit $startpos,$eachpage ";
}
//----------------------------------------------------------------------------------------|
//显示分页
//参数说明:$cx为URL后的相关参数,如果在分页时需要附加相关参数,则在此加入,并且以&开头,以便形成?page=*&...
function showpage($cx="")
{
global $page,$pages,$prepage,$nextpage,$queryString,$sums;
$shownum =10;
      if ($page<=ceil($shownum/2))
      {
            $startpage = 1;
            $endpage = ($shownum+1<=$pages)? $shownum+1: $pages;
      }
      else if($page>=$pages){
            $endpage = $pages;
            $startpage = ($pages-$shownum<=1)? 1 : $pages-$shownum;
      }else{
            $startpage = $page-ceil($shownum/2);
            $endpage = $page+ceil($shownum/2);
            if ($endpage >$pages)
            {
                  $startpage = $pages-$shownum;
                  $endpage = $pages;
            }
      }
$showpage = "<div class=\"xspace-page\">";
$showpage .= "<span class=\"xspace-totlerecord\">共 ".$sums." 条记录</span> <span class=\"xspace-totlepages\">共 ".($page).'/'.($pages)." 页</span> ";
if($page>1) $showpage .= " <a href=$href?page=1$cx>|<</a> ";
if($startpage>1)
$showpage .= " <a href=$href?page=".($page-1)."$cx><</a> ";
for($i=$startpage;$i<=$endpage;$i++)
{
if($i==$page) $showpage .= " <span class=\"xspace-current\" style='color:red;'>".($i)."</span> ";
else $showpage .= " <a href=$href?page=$i$cx>".($i)."</a> ";
}
if($endpage<$pages)
$showpage .= " <a href=$href?page=".($page+1)."$cx>></a> ";
if($page<$pages)
$showpage .= " <a href=$href?page=$pages$cx>>|</a> ";
      $showpage .= " 转到 <input name='goto_page' id='goto_page' type='text' value='$page' style='width:20px;' onchange=\"document.getElementById('goto_page').value=this.value;\" /><input name='btn' type='button' value='G' onclick=\"location.href='$href?page='+document.all.goto_page.value+'$cx'\" />";
      $showpage .= "</div>";
      return ($showpage);
}
?>
文章来源:桂林唯创网络
免责声明:本站相关技术文章信息部分来自网络,目的主要是传播更多信息,如果您认为本站的某些信息侵犯了您的版权,请与我们联系,我们会即时妥善的处理,谢谢合作!