Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4052151
  • 博文数量: 272
  • 博客积分: 7846
  • 博客等级: 少将
  • 技术积分: 6476
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-25 16:27
文章分类

全部博文(272)

分类: 系统运维

2011-10-15 13:00:49

    


    基地购置了指纹考勤机,昨天打扫卫生又杀虫子,这东西试了两下也没研究明白,今天十年梦生忙我搞定,并破译了考勤机数据库密码。其实这东西也不是他们自行开发的,起码软件的数据库内容都是从国外的同类东西,把部分汉化过来的,可以看到数据库中有很多表都是全英文的信息。


    



    虽然考勤机自带的软件功能不错了,但为了定制,所以如果想开发出针对领导只看到按日期的员工出勤的统计报表的话,那就只有自己研究数据库并自己写代码了。
    以前没做过PHP访问access数据的东西,这次碰到,搜了一下,发现一篇博文记录了一个access数据库访问类,不错,摘录下来。
  1. <?php
  2. class db_access
  3. {
  4.     var $querynum = 0;
  5.     var $conn;
  6.     var $insertid = 0;
  7.     var $cursor = 0;

  8.     function connect($dbhost, $dbuser = '', $dbpw = '', $dbname = '', $pconnect = 0)
  9.     {
  10.         $this->conn = new com('adodb.connection');
  11.         if(!$this->conn) return false;
  12.         //    or exit('Cannot start ADO');
  13.         $this->conn->open("DRIVER={Microsoft Access Driver (*.mdb)};dbq=$dbhost;uid=$dbuser;pwd=$dbpw");
  14.         if($this->conn->state == 0)
  15.         {
  16.             $this->conn->open("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=$dbhost");
  17.             if($this->conn->state == 0)    return false;;
  18.         }
  19.         define('NUM', 1);
  20.         define('ASSOC', 2);
  21.         define('BOTH', 3);
  22.         return $this->conn->state;
  23.     }


  24.     function select_db($dbname)
  25.     {
  26.         return $this->conn->state;
  27.     }

  28.     function query($sql, $type = '', $expires = 3600, $dbname = '')
  29.     {
  30.         $this->querynum++;
  31.         $sql = trim($sql);
  32.         if(preg_match("/^(select.*)limit ([0-9]+)(,([0-9]+))?$/i", $sql, $matchs))
  33.         {
  34.             $sql = $matchs[1];
  35.             $offset = $matchs[2];
  36.             $pagesize = $matchs[4];
  37.             $query = $this->conn->Execute($sql);
  38.             return $this->limit($query, $offset, $pagesize);
  39.         }
  40.         else
  41.         {
  42.             return $this->conn->Execute($sql);
  43.         }
  44.     }

  45.     function get_one($query)
  46.     {
  47.         $this->querynum++;
  48.      $rs = $this->conn->Execute($query);
  49.         $r = $this->fetch_array($rs);
  50.         $this->free_result($rs);
  51.         return $r;
  52.     }

  53.     function fetch_array($rs, $result_type = 3)
  54.     {
  55.         if(is_array($rs))
  56.         {
  57.             return $this->cursor < count($rs) ? $rs[$this->cursor++] : FALSE;
  58.         }
  59.         else
  60.         {
  61.             if($rs->EOF) return FALSE;
  62.             $array = array();
  63.             for($i = 0; $i < $this->num_fields($rs); $i++)
  64.             {
  65.                 $fielddata = $rs->Fields[$i]->Value;
  66.              if($result_type == NUM || $result_type == BOTH) $array[$i] = $fielddata;
  67.              if($result_type == ASSOC || $result_type == BOTH) $array[$rs->Fields[$i]->Name] = $fielddata;
  68.             }
  69.             $rs->MoveNext();
  70.             return $array;
  71.         }
  72.     }
  73.     
  74.     function select($sql, $keyfield = '')
  75.     {
  76.         $array = array();
  77.         $result = $this->query($sql);
  78.         while($r = $this->fetch_array($result))
  79.         {
  80.             if($keyfield)
  81.             {
  82.                 $key = $r[$keyfield];
  83.                 $array[$key] = $r;
  84.             }
  85.             else
  86.             {
  87.                 $array[] = $r;
  88.             }
  89.         }
  90.         $this->free_result($result);
  91.         return $array;
  92.     }

  93.     function num_rows($rs)
  94.     {
  95.      return is_array($rs) ? count($rs) : $rs->recordcount;
  96.     }

  97.     function num_fields($rs)
  98.     {
  99.      return $rs->Fields->Count;
  100.     }

  101.     function fetch_assoc($rs)
  102.     {
  103.      return $this->fetch_array($rs, ASSOC);
  104.     }

  105.     function fetch_row($rs)
  106.     {
  107.      return $this->fetch_array($rs, NUM);
  108.     }

  109.     function free_result($rs)
  110.     {
  111.      if(is_resource($rs)) $rs->close();
  112.     }

  113.     function error()
  114.     {
  115.      return $this->conn->Errors[$this->conn->Errors->Count-1]->Number;
  116.     }

  117.     function errormsg()
  118.     {
  119.      return $this->conn->Errors[$this->conn->Errors->Count-1]->Description;
  120.     }

  121.     function close()
  122.     {
  123.      $this->conn->close();
  124.     }

  125.     function limit($rs, $offset, $pagesize = 0)
  126.     {
  127.         if($pagesize > 0)
  128.         {
  129.             $rs->Move($offset);
  130.         }
  131.         else
  132.         {
  133.             $pagesize = $offset;
  134.         }
  135.         $info = array();
  136.         for($i = 0; $i < $pagesize; $i++)
  137.         {
  138.             $r = $this->fetch_array($rs);
  139.             if(!$r) break;
  140.             $info[] = $r;
  141.         }
  142.         $this->free_result($rs);
  143.         $this->cursor = 0;
  144.         return $info;
  145.     }
  146. }
  147. ?>

    进一步还有例子:(感谢
  1. <?php
  2. define('ACC_ROOT', str_replace("\\", '/', dirname(__FILE__)).'/');//定义本程序根路径
  3. define('DB_HOST',ACC_ROOT.'#%201f9558a613f476d0d591.mdb');//定义access存放路径
  4. include(ACC_ROOT.'access/db_access.class.php');//载入access类库
  5. $blogdb = new db_access;//实例化
  6. $blogdb->connect(DB_HOST, DB_USER, DB_PW, DB_NAME, DB_PCONNECT, DB_CHARSET);//开启access链接
  7. $execarr = array();
  8. $sqlblog = $blogdb->query("select log_ID,log_Title,log_Intro,log_PostTime,log_CateID from blog_Article order by log_PostTime DESC limit 0,5");
  9. while($arrblog = $blogdb->fetch_array($sqlblog))
  10.     {
  11.     $execarr[] = $arrblog;
  12.     }
  13. $cat = array(3=>'easay',4=>'video',5=>'bridalveil',6=>'activity',8=>'photo',9=>'media',10=>'wedding');//定义url链接目录
  14. $ext = 'gif|jpg|jpeg|bmp|png';//罗列图片后缀从而实现多扩展名匹配
  15. foreach($execarr AS $k=>$v)
  16. {
  17. preg_match_all("/(href|src)=([\"|']?)([^ \"'>]+\.($ext))\\2/i", $v['log_Intro'], $matches);
  18.               $imgsrc = ''.str_replace('upload','attachments',$matches[3][0]);
  19. echo '
  20. .$cat[$v['log_CateID']].'/'.$v['log_ID'].'.html" target="_blank">
  21.                         .$imgsrc.'" width="87" height="58" />
  22.                         

    '.$v['log_Title'].'


  23.                         

    POST TIME:'.$v['log_PostTime'].'

  24. ';
  25. }
  26. ?>
阅读(3240) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~