mysql.php 19 KB

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