mysql.php 18 KB

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