mysql.php 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759
  1. <?php
  2. /**
  3. *****************************************************************
  4. * 联系QQ: 290802026 *
  5. * 版 本: V2.0 *
  6. * 开发者:雨中磐石工作室 *
  7. * 网 址: http://www.rockoa.com/ *
  8. * 说 明: 数据库核心类 *
  9. * 备 注: 未经允许不得商业出售,代码欢迎参考纠正 *
  10. *****************************************************************
  11. */
  12. if(!defined('HOST'))exit('not access');
  13. abstract class mysql{
  14. public $conn = null;
  15. public $iudcount = 0;
  16. public $iudarr = array();
  17. public $tran = false;
  18. public $rock;
  19. public $nowsql;
  20. public $countsql = 0;
  21. public $sqlarr = array();
  22. public $total = 0;
  23. public $count = 0;
  24. public $perfix = PREFIX;
  25. public $errorbool = false;
  26. public $errormsg = '';
  27. public $errorlast = '';
  28. public $nowerror = false;
  29. public $basename;
  30. protected $db_host;
  31. protected $db_user;
  32. protected $db_pass;
  33. protected $db_base;
  34. protected $exparray = array(
  35. 'eq' => "='?0'", 'neq' => "<>'?0'", 'eqi' => '=?0', 'neqi' => '<>?0', 'lt' => "<'?0'", 'elt' => "<='?0'",
  36. 'gt' => ">'?0'", 'egt' => ">='?0'", 'lti' => '<?0', 'elti' => '<=?0', 'gti' => '>?0', 'egti' => '>=?0',
  37. 'like' => "LIKE '%?0%'", 'notlike' => "NOT LIKE '%?0%'", 'leftlike' => "LIKE '%?0'", 'rightlike' => "LIKE '?0%'",
  38. 'in' => "IN(?0)", 'notin' => "NOT IN(?0)",
  39. 'between' => "BETWEEN '?0' AND '?1'", 'notbetween' => "NOT BETWEEN '?0' AND '?1'",
  40. 'betweeni' => "BETWEEN ?0 AND ?1", 'notbetweeni' => "NOT BETWEEN ?0 AND ?1"
  41. );
  42. //sql中禁用方法
  43. protected $disabledfua = array('dumpfile','outfile','load_file','system_user');
  44. public function __construct()
  45. {
  46. $this->rock = $GLOBALS['rock'];
  47. $this->errorbool = false;
  48. $this->errormsg = '';
  49. if(getconfig('dbencrypt')){
  50. $this->db_host = $this->rock->jm->uncrypt(DB_HOST);
  51. $this->db_user = $this->rock->jm->uncrypt(DB_USER);
  52. $this->db_pass = $this->rock->jm->uncrypt(DB_PASS);
  53. $this->db_base = $this->rock->jm->uncrypt(DB_BASE);
  54. }else{
  55. $this->db_host = DB_HOST;
  56. $this->db_user = DB_USER;
  57. $this->db_pass = DB_PASS;
  58. $this->db_base = DB_BASE;
  59. }
  60. $this->basename = $this->db_base;
  61. }
  62. public function __destruct()
  63. {
  64. if($this->conn){
  65. $this->tranend();
  66. $this->close();
  67. }
  68. //记录访问sql日志
  69. if(getconfig('sqllog')){
  70. $sql = '';
  71. $filstr = 'sqllog_'.date('Y.m.d.H.i.s').'_'.$this->rock->adminid.'_'.str_shuffle('abcdefghijklmn').'.log';
  72. foreach($this->sqlarr as $sql1)$sql.="\n\n$sql1;";
  73. if($sql!='')$this->rock->createtxt(''.UPDIR.'/sqllog/'.date('Y-m-d').'/'.$filstr.'', "时间[".$this->rock->now."],用户[".$this->rock->adminid.".".$this->rock->adminname."],IP[".$this->rock->ip."],WEB[".$this->rock->web."],URL[".$this->rock->nowurl()."]".$sql);
  74. }
  75. }
  76. protected function connect(){}
  77. protected function selectdb($name)
  78. {
  79. $this->basename = $name;
  80. }
  81. protected function querysql($sql){return false;}
  82. protected function starttran(){}
  83. protected function endtran($bo){}
  84. public function fetch_array($res, $type=0){return false;}
  85. public function insert_id(){return 0;}
  86. public function error(){return '';}
  87. public function close(){}
  88. public function changeattr($host, $user, $pass, $base)
  89. {
  90. $this->db_host = $host;
  91. $this->db_user = $user;
  92. $this->db_pass = $pass;
  93. $this->db_base = $base;
  94. }
  95. public function connectdb()
  96. {
  97. $this->errormsg = '';
  98. $this->connect();
  99. return $this->conn;
  100. }
  101. public function query($sql, $ebo=true)
  102. {
  103. if($this->conn == null)$this->connect();
  104. if($this->conn == null)exit('数据库的帐号/密码有错误!'.$this->errormsg.'');
  105. $sql = trim($sql);
  106. $sql = str_replace(array('[Q]','[q]','{asqom}'), array($this->perfix, $this->perfix,''), $sql);
  107. $sqls = strtolower($sql);
  108. foreach($this->disabledfua as $fus)if(contain($sqls,$fus))exit('禁止包含'.$fus.'字符串');
  109. $this->countsql++;
  110. $this->sqlarr[] = $sql;
  111. $this->nowsql = $sql;
  112. $this->count = 0;
  113. try {
  114. $rsbool = $this->querysql($sql);
  115. } catch (Exception $e) {
  116. $rsbool = false;
  117. $this->errormsg = $e->getMessage();
  118. }
  119. $this->nowerror = false;
  120. if(!$rsbool)$this->nowerror = true;
  121. $stabs = ''.$this->perfix.'log';
  122. if(!contain($sql, $stabs) && !$rsbool)$this->errorlast = $this->error(); //最后错误信息
  123. //记录错误sql
  124. if(!$rsbool && $ebo){
  125. $txt = '[ERROR SQL]'.chr(10).''.$sql.''.chr(10).''.chr(10).'[Reason]'.chr(10).''.$this->error().''.chr(10).'';
  126. $efile = $this->rock->debug($txt,''.DB_DRIVE.'_sqlerr', true);
  127. $errmsg = str_replace("'",'&#39;', $this->error());
  128. if(!contain($sql, $stabs)){
  129. m('log')->addlogs('错误SQL',''.$errmsg.'', 2, array(
  130. 'url' => $efile
  131. ));
  132. }
  133. }
  134. return $rsbool;
  135. }
  136. /**
  137. * 返回最后错误信息
  138. */
  139. public function lasterror()
  140. {
  141. $err = $this->errorlast;
  142. if($err=='')$err = $this->error();
  143. return $err;
  144. }
  145. public function execsql($sql)
  146. {
  147. $rsa = $this->query($sql);
  148. $this->iudarr[]=$rsa;
  149. return $rsa;
  150. }
  151. public function getLastSql()
  152. {
  153. return $this->nowsql;
  154. }
  155. public function getsyscount($lx='')
  156. {
  157. $to = 0;
  158. if($lx=='')return $to;
  159. $lx = strtoupper($lx);
  160. $rsa = $this->getall('SELECT '.$lx.'() as total');
  161. $to = $rsa[0]['total'];
  162. return $to;
  163. }
  164. /**
  165. * 返回使用SQL_CALC_FOUND_ROWS,统计总记录数
  166. */
  167. public function found_rows()
  168. {
  169. return $this->getsyscount('found_rows');
  170. }
  171. /**
  172. * 返回update,insert,delete上所影响的条数
  173. */
  174. public function row_count()
  175. {
  176. return $this->getsyscount('row_count');
  177. }
  178. /**
  179. * 获取select的sql
  180. */
  181. public function getsql($arr=array())
  182. {
  183. $where = $table = $order = $limit = $group = '';
  184. $fields = '*';
  185. if(isset($arr['table']))$table=$arr['table'];
  186. if(isset($arr['where']))$where=$arr['where'];
  187. if(isset($arr['order']))$order=$arr['order'];
  188. if(isset($arr['limit']))$limit=$arr['limit'];
  189. if(isset($arr['group']))$group=$arr['group'];
  190. if(isset($arr['fields']))$fields=$arr['fields'];
  191. $where = $this->getwhere($where);
  192. $table = $this->gettable($table);
  193. $sql = "SELECT $fields FROM $table";
  194. if($where!=''){
  195. //$where = $this->filterstr($where);
  196. $sql.=" WHERE $where";
  197. }
  198. if($order!='')$sql.=" ORDER BY $order";
  199. if($group!='')$sql.=" GROUP BY $group";
  200. if($limit!='')$sql.=" LIMIT $limit";
  201. return $sql;
  202. }
  203. //弃用过滤
  204. public function filterstr($str)
  205. {
  206. $str = strtolower($str);
  207. $file= explode(',','delete,drop,update,union,exec,insert,declare,master,truncate,create,alter,database');
  208. $res = array();
  209. foreach($file as $fid)$res[]='';
  210. $str = str_replace($file, $res, $str);
  211. return $str;
  212. }
  213. public function getone($table,$where,$fields='*',$order='')
  214. {
  215. $rows = $this->getrows($table,$where,$fields,$order,'1');
  216. $row = false;
  217. if($this->count>0)$row=$rows[0];
  218. return $row;
  219. }
  220. public function getrows($table,$where,$fields='*',$order='', $limit='',$group='')
  221. {
  222. $sql = $this->getsql(array(
  223. 'table' => $table,
  224. 'where' => $where,
  225. 'fields'=> $fields,
  226. 'order' => $order,
  227. 'limit' => $limit,
  228. 'group' => $group
  229. ));
  230. return $this->getall($sql);
  231. }
  232. public function getall($sql)
  233. {
  234. $res=$this->query($sql);
  235. $arr=array();
  236. if($res){
  237. while($row=$this->fetch_array($res)){
  238. $arr[] = $row;
  239. $this->count++;
  240. }
  241. }
  242. return $arr;
  243. }
  244. /**
  245. string table1 a left JOIN table2 b on b.uid=a.id
  246. array(table=>$table,join=>'left')
  247. */
  248. public function gettable($arr)
  249. {
  250. if(is_array($arr)){
  251. $s = '';$oi=0;
  252. foreach($arr as $k=>$v){
  253. if($oi==0){
  254. $s=''.$v.' a';
  255. }else{
  256. if($k=='join')$s.=' '.$v.' JOIN';
  257. if($k=='table1')$s.=' '.$v.' b';
  258. if($k=='where')$s.=' ON '.$v.'';
  259. if($k=='where1')$s.=' AND '.$v.'';
  260. }
  261. $oi++;
  262. }
  263. $arr = $s;
  264. }
  265. return $arr;
  266. }
  267. /**
  268. 条件的
  269. $arrs = array(
  270. 'id|eqi|a' => '0',
  271. 'name|like' => '我',
  272. 'id|notin' => '0,12',
  273. 'enddt|rightlike' => '2015-10',
  274. 'startdt|between' => '2015-10-01@@@2015-10-31',
  275. 'price|notbetweeni' => '1@@@10',
  276. 'sid > ?0 and <?1' => '0@@@2'
  277. );
  278. */
  279. public function getwhere($where='')
  280. {
  281. $len = func_num_args();
  282. $arr = array();
  283. $sfh1 = '';
  284. for($i=0; $i<$len; $i++){
  285. $sfh = func_get_arg($i);
  286. if(is_numeric($sfh)){
  287. $arr[] = "`id`='$sfh'";
  288. }else if($sfh=='AND' || $sfh=='OR' || $sfh=='and' || $sfh=='or'){
  289. $sfh1 = $sfh;
  290. }else{
  291. $arr[] = $this->_getwhere($sfh);
  292. }
  293. }
  294. $joins = ') AND (';
  295. if($sfh1!='')$joins = ') '.$sfh1.' (';
  296. $where = join($joins, $arr);
  297. if($sfh1!='')$where = "($where)";
  298. return $where;
  299. }
  300. private function _getwhere($where='')
  301. {
  302. if($where=='')return '';
  303. if(is_numeric($where)){
  304. $where = "`id`='$where'";
  305. }else if(is_array($where)){
  306. $sarr = array();
  307. foreach($where as $fid=>$val){
  308. $qz = '';
  309. $farr = explode('|', $fid);
  310. $fid = $farr[0];
  311. $_fhs = "='?0'";
  312. if(isset($farr[1])){
  313. $_fh = $farr[1];
  314. if(isset($this->exparray[$_fh]))$_fhs=$this->exparray[$_fh];
  315. }
  316. if(isset($farr[2]))$qz=''.$farr[2].'.';
  317. $vala = explode('@@@', $val);
  318. $val1 = $vala[0];$val2='';
  319. if(isset($vala[1]))$val2=$vala[1];
  320. $_bo1 = $this->contain($fid,'?0');
  321. if($_bo1)$_fhs = $fid;
  322. $_fhs = str_replace(array('?0','?1'), array($val1,$val2), $_fhs);
  323. $s = $_fhs;
  324. if(!$_bo1)$s = ''.$qz.'`'.$fid.'` '.$_fhs.'';
  325. $sarr[]=$s;
  326. }
  327. $where = join(' AND ', $sarr);
  328. }
  329. return $where;
  330. }
  331. /**
  332. * 以$kfied作为主键返回数组
  333. */
  334. public function getarr($table, $where='', $fields='*', $kfied='id')
  335. {
  336. $sql = $this->getsql(array(
  337. 'table' => $table,
  338. 'where' => $where,
  339. 'fields'=> "`$kfied`,$fields"
  340. ));
  341. $res = $this->query($sql);
  342. $arr = array();
  343. if($res){
  344. while($row=$this->fetch_array($res)){
  345. $arr[$row[$kfied]] = $row;
  346. $this->count++;
  347. }
  348. }
  349. return $arr;
  350. }
  351. /**
  352. 读取全部同时将第一个字段作为主键(读取的数据存在数组里)
  353. */
  354. public function getkeyall($table,$fields,$where='')
  355. {
  356. $sql = $this->getsql(array(
  357. 'table' => $table,
  358. 'where' => $where,
  359. 'fields'=> $fields
  360. ));
  361. $res=$this->query($sql);
  362. $arr=array();
  363. if($res){
  364. while(list($ka,$ab) = $this->fetch_array($res, 1)){
  365. $arr[$ka]=$ab;
  366. $this->count++;
  367. }
  368. }
  369. return $arr;
  370. }
  371. /**
  372. 读取一条sql语句用规定字符连接起来
  373. */
  374. public function getjoinval($table,$fields,$where='',$join=',')
  375. {
  376. $sql = $this->getsql(array(
  377. 'table' => $table,
  378. 'where' => $where,
  379. 'fields'=> $fields
  380. ));
  381. $res=$this->query($sql);
  382. $arr=array();
  383. if($res){
  384. while(list($kv) = $this->fetch_array($res, 1)){
  385. $arr[]=$kv;
  386. $this->count++;
  387. }
  388. }
  389. return join($join,$arr);
  390. }
  391. /**
  392. 读取某行某字段的
  393. */
  394. public function getmou($table,$fields,$where,$order='')
  395. {
  396. $sql = $this->getsql(array(
  397. 'table' => $table,
  398. 'where' => $where,
  399. 'fields'=> $fields,
  400. 'order' => $order
  401. ));
  402. $res=$this->query($sql);
  403. if($res){
  404. $row = $this->fetch_array($res, 1);
  405. if($row){
  406. $this->count = 1;
  407. return $row[0];
  408. }
  409. }
  410. return false;
  411. }
  412. /**
  413. * 开启事务
  414. */
  415. public function routinestart()
  416. {
  417. $this->starttran();
  418. }
  419. /**
  420. * 提交/回滚事务
  421. * $bo=null 自动 true 提交,false 回滚
  422. */
  423. public function routineend($bo=null)
  424. {
  425. if(!is_bool($bo))$bo = $this->backsql();
  426. $this->endtran($bo);
  427. return $bo;
  428. }
  429. /**
  430. * 启用事务,没有事务
  431. */
  432. private function tranbegin($sql)
  433. {
  434. //if($this->errorbool)return false;
  435. if($this->conn == null)$this->connect();
  436. $this->iudcount++;
  437. if(!$this->tran){
  438. //$this->starttran();
  439. //$this->tran=true;
  440. }
  441. $rsa = $this->query($sql);
  442. $this->iudarr[]=$rsa;
  443. if(!$rsa)$this->errorbool = true;
  444. return $rsa;
  445. }
  446. /**
  447. 事务结束
  448. */
  449. private function tranend()
  450. {
  451. if($this->tran){
  452. //$this->endtran($this->backsql());
  453. }
  454. $this->tran=false;
  455. }
  456. /**
  457. 判断插入更新删除sql语句是否有错
  458. */
  459. public function backsql()
  460. {
  461. $subt=true;
  462. foreach($this->iudarr as $tra){
  463. if(!$tra){
  464. $subt=false;
  465. break;
  466. }
  467. }
  468. return $subt;
  469. }
  470. public function insert($table,$name,$values,$sel=false)
  471. {
  472. $sql="insert into `$table` ($name) ";
  473. if(!$sel){
  474. $sql.="values($values)";
  475. }else{
  476. $sql.=$values;
  477. }
  478. return $this->tranbegin($sql);
  479. }
  480. public function update($table,$content,$where)
  481. {
  482. $where = $this->getwhere($where);
  483. $sql="update `$table` set $content where $where ";
  484. return $this->tranbegin($sql);
  485. }
  486. public function delete($table,$where)
  487. {
  488. $where = $this->getwhere($where);
  489. $sql="delete from `$table` where $where ";
  490. return $this->tranbegin($sql);
  491. }
  492. /**
  493. 记录添加修改
  494. */
  495. public function record($table,$array,$where='')
  496. {
  497. $addbool = true;
  498. if(!$this->isempt($where))$addbool=false;
  499. $cont = '';
  500. if(is_array($array)){
  501. foreach($array as $key=>$val){
  502. $cont.=",`$key`=".$this->toaddval($val)."";
  503. }
  504. $cont = substr($cont,1);
  505. }else{
  506. $cont = $array;
  507. }
  508. if($addbool){
  509. $sql="insert into `$table` set $cont";
  510. }else{
  511. $where = $this->getwhere($where);
  512. $sql="update `$table` set $cont where $where";
  513. }
  514. return $this->tranbegin($sql);
  515. }
  516. /**
  517. 返回总条数
  518. */
  519. public function rows($table,$where,$rowtype='count(1)'){
  520. return (int)$this->getmou($table,$rowtype,$where);
  521. }
  522. /**
  523. 返回所有数据库的表
  524. */
  525. public function getalltable($base='')
  526. {
  527. if($base=='')$base = $this->basename;
  528. $sql = "select `TABLE_NAME` from information_schema.`TABLES` where `TABLE_SCHEMA`='$base'";
  529. $arr = $this->getall($sql);
  530. $rows= array();
  531. foreach($arr as $k=>$rs)$rows[] = $rs['TABLE_NAME'];
  532. return $rows;
  533. }
  534. /**
  535. 返回表所有字段
  536. */
  537. public function getallfields($table)
  538. {
  539. $finfo = $this->gettablefields($table);
  540. foreach ($finfo as $val) {
  541. $arr[] = $val['name'];
  542. }
  543. return $arr;
  544. }
  545. public function getfields($table)
  546. {
  547. $f = $this->getallfields($table);
  548. foreach($f as $f1)$arr[$f1]='';
  549. return $arr;
  550. }
  551. public function gettablefields($table, $base='',$whe='')
  552. {
  553. if($base=='')$base = $this->db_base;
  554. $sql = "select COLUMN_NAME as `name`,DATA_TYPE as `type`,COLUMN_COMMENT as `explain`,COLUMN_TYPE as `types`,`COLUMN_DEFAULT` as dev,`IS_NULLABLE` as isnull,`CHARACTER_MAXIMUM_LENGTH` as lens,`NUMERIC_PRECISION` as xslen1,`NUMERIC_SCALE` as xslen2 from information_schema.COLUMNS where `TABLE_NAME`='$table' and `TABLE_SCHEMA` ='$base' $whe order by `ORDINAL_POSITION`";
  555. return $this->getall($sql);
  556. }
  557. /**
  558. 读取表结构
  559. */
  560. public function gettablecolumn($table, $fields='')
  561. {
  562. $where = '';
  563. if($fields!='')$where = "and `COLUMN_NAME`='$fields'";
  564. $sql = "select COLUMN_NAME as `name`,DATA_TYPE as `type`,COLUMN_COMMENT as `explain`,COLUMN_TYPE as `types`,COLUMN_DEFAULT as 'defval' from information_schema.COLUMNS where `TABLE_NAME`='$table' and `TABLE_SCHEMA` ='$this->db_base' $where order by `ORDINAL_POSITION`";
  565. $arr = $this->getall($sql);
  566. $rows = array();
  567. foreach($arr as $k=>$rs){
  568. $dev = 'NULL';
  569. if(!$this->isempt($rs['defval']))$dev=$rs['defval'];
  570. $str = "`".$rs['name']."` ".$rs['types']." DEFAULT ".$dev."";
  571. if(!$this->isempt($rs['explain']))$str.=" COMMENT '".$rs['explain']."'";
  572. $rows[] = $str;
  573. }
  574. return $rows;
  575. }
  576. public function showcreatetable($table)
  577. {
  578. $sql = "show create table `$table`";
  579. $res= $this->query($sql);
  580. list($ka,$nr) = $this->fetch_array($res, 1);
  581. return $nr;
  582. }
  583. /**
  584. 判断变量是否为空
  585. */
  586. public function isempt($str)
  587. {
  588. return isempt($str);
  589. }
  590. public function contain($str,$a)
  591. {
  592. $bool=false;
  593. if(!$this->isempt($a) && !$this->isempt($str)){
  594. $ad=strpos($str,$a);
  595. if($ad>0||!is_bool($ad))$bool=true;
  596. }
  597. return $bool;
  598. }
  599. /**
  600. 转换数据库可插入的对象
  601. */
  602. public function toaddval($str)
  603. {
  604. $adstr="'$str'";
  605. if($this->isempt($str)){
  606. $adstr='null';
  607. }else{
  608. if(substr($str,0,4)=='(&;)')$adstr=substr($str,4);
  609. }
  610. return $adstr;
  611. }
  612. /**
  613. * 替换特殊符合'
  614. */
  615. public function tocovexec($str, $lx=0)
  616. {
  617. $str = str_replace('\'', '&#39;',$str);
  618. if($lx==1){
  619. $str = str_replace("\n", '',$str);
  620. }
  621. return $str;
  622. }
  623. /**
  624. 创建随机编号
  625. */
  626. public function ranknum($table,$field='num',$n=6, $dx=0)
  627. {
  628. $arr = array('0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
  629. $num = '';
  630. for($i=1;$i<=$n;$i++)$num.=$arr[rand(0,count($arr)-1)];
  631. if($dx==1)$num = strtoupper($num);//转换成大写
  632. $rsnum = $this->getmou($table,$field,"`$field`='$num'");
  633. return ($rsnum)?$this->ranknum($table,$field,$n, $dx):$num;
  634. }
  635. /**
  636. 流水编号
  637. */
  638. public function sericnum($num, $table,$fields='sericnum', $ws=4, $whe='')
  639. {
  640. $dts = explode('-', date('Y-m-d'));
  641. $ymd = $dts[0].$dts[1].$dts[2];
  642. $ym = $dts[0].$dts[1];
  643. $num = str_replace('Ymd', $ymd, $num);
  644. $num = str_replace('Ym', $ym, $num);
  645. $num = str_replace('Year', $dts[0], $num);
  646. $num = str_replace('Day', $dts[2], $num);
  647. $num = str_replace('Month', $dts[1], $num);
  648. $where = "`$fields` like '".$num."%' $whe";
  649. $max = (int)$this->getmou($table, "max(cast(replace(`$fields`,'$num','') as decimal(10)))", $where);
  650. $max++;
  651. $wsnum = ''.$max.'';
  652. $len = strlen($wsnum);
  653. $oix = $ws - $len;
  654. for($i=1;$i<=$oix;$i++)$wsnum='0'.$wsnum;
  655. $num .= $wsnum;
  656. return $num;
  657. }
  658. /**
  659. * 获取所有顶级信息连接起来
  660. * @param $table 表名
  661. * @param $pfields 上级字段 $jfield 要连接的字段名 $afid = 值
  662. */
  663. private $joinarr=array();
  664. public function getpval($table,$pfields,$jfield,$afid,$plit='/',$afield='id',$maxlen=8)
  665. {
  666. $this->joinarr = array();
  667. $this->joinlen = 0;
  668. $this->getpvala($table,$pfields,$jfield,$afid,$afield,$maxlen);
  669. return join($plit,array_reverse($this->joinarr));
  670. }
  671. private function getpvala($table,$pfields,$jfield,$afid,$afield,$maxlen)
  672. {
  673. if(count($this->joinarr)>=$maxlen)return;
  674. $rsa = $this->getone($table,"`$afield`='$afid'","`id`,`$pfields`,`$jfield`");
  675. if($rsa){
  676. $this->joinarr[]=$rsa[$jfield];
  677. $pid = $rsa[$pfields];
  678. if($pid!=$afid)if($this->rows($table,"`$afield`='$pid'")>0)$this->getpvala($table,$pfields,$jfield,$pid,$afield,$maxlen);
  679. }
  680. }
  681. }
  682. class DB{
  683. public static $tablename;
  684. public static function table($tab)
  685. {
  686. self::$tablename = ''.getconfig('.perfix.').$tab.'';
  687. return m($tab);
  688. }
  689. public static function where($f, $v)
  690. {
  691. }
  692. }