mysql.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838
  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, $cans=array())
  249. {
  250. if(is_array($sql))$sql = $this->getsql($sql);
  251. $res=$this->query($sql);
  252. $arr=array();
  253. if($res){
  254. while($row=$this->fetch_array($res)){
  255. if($call != null)$row = $call($row, $cans);
  256. $arr[] = $row;
  257. $this->count++;
  258. }
  259. }
  260. return $arr;
  261. }
  262. /**
  263. string table1 a left JOIN table2 b on b.uid=a.id
  264. array(table=>$table,join=>'left')
  265. */
  266. public function gettable($arr)
  267. {
  268. if(is_array($arr)){
  269. $s = '';$oi=0;
  270. foreach($arr as $k=>$v){
  271. if($oi==0){
  272. $s=''.$v.' a';
  273. }else{
  274. if($k=='join')$s.=' '.$v.' JOIN';
  275. if($k=='table1')$s.=' '.$v.' b';
  276. if($k=='where')$s.=' ON '.$v.'';
  277. if($k=='where1')$s.=' AND '.$v.'';
  278. }
  279. $oi++;
  280. }
  281. $arr = $s;
  282. }
  283. return $arr;
  284. }
  285. /**
  286. 条件的
  287. $arrs = array(
  288. 'id|eqi|a' => '0',
  289. 'name|like' => '我',
  290. 'id|notin' => '0,12',
  291. 'enddt|rightlike' => '2015-10',
  292. 'startdt|between' => '2015-10-01@@@2015-10-31',
  293. 'price|notbetweeni' => '1@@@10',
  294. 'sid > ?0 and <?1' => '0@@@2'
  295. );
  296. */
  297. public function getwhere($where='')
  298. {
  299. $len = func_num_args();
  300. $arr = array();
  301. $sfh1 = '';
  302. for($i=0; $i<$len; $i++){
  303. $sfh = func_get_arg($i);
  304. if(is_numeric($sfh)){
  305. $arr[] = "`id`='$sfh'";
  306. }else if($sfh=='AND' || $sfh=='OR' || $sfh=='and' || $sfh=='or'){
  307. $sfh1 = $sfh;
  308. }else{
  309. $arr[] = $this->_getwhere($sfh);
  310. }
  311. }
  312. $joins = ') AND (';
  313. if($sfh1!='')$joins = ') '.$sfh1.' (';
  314. $where = join($joins, $arr);
  315. if($sfh1!='')$where = "($where)";
  316. return $where;
  317. }
  318. private function _getwhere($where='')
  319. {
  320. if($where=='')return '';
  321. if(is_numeric($where)){
  322. $where = "`id`='$where'";
  323. }else if(is_array($where)){
  324. $sarr = array();
  325. foreach($where as $fid=>$val){
  326. $qz = '';
  327. $farr = explode('|', $fid);
  328. $fid = $farr[0];
  329. $_fhs = "='?0'";
  330. if(isset($farr[1])){
  331. $_fh = $farr[1];
  332. if(isset($this->exparray[$_fh]))$_fhs=$this->exparray[$_fh];
  333. }
  334. if(isset($farr[2]))$qz=''.$farr[2].'.';
  335. $vala = explode('@@@', $val);
  336. $val1 = $vala[0];$val2='';
  337. if(isset($vala[1]))$val2=$vala[1];
  338. $_bo1 = $this->contain($fid,'?0');
  339. if($_bo1)$_fhs = $fid;
  340. $_fhs = str_replace(array('?0','?1'), array($val1,$val2), $_fhs);
  341. $s = $_fhs;
  342. if(!$_bo1)$s = ''.$qz.'`'.$fid.'` '.$_fhs.'';
  343. $sarr[]=$s;
  344. }
  345. $where = join(' AND ', $sarr);
  346. }
  347. return $where;
  348. }
  349. /**
  350. * 以$kfied作为主键返回数组
  351. */
  352. public function getarr($table, $where='', $fields='*', $kfied='id')
  353. {
  354. $sql = $this->getsql(array(
  355. 'table' => $table,
  356. 'where' => $where,
  357. 'fields'=> "`$kfied`,$fields"
  358. ));
  359. $res = $this->query($sql);
  360. $arr = array();
  361. if($res){
  362. while($row=$this->fetch_array($res)){
  363. $arr[$row[$kfied]] = $row;
  364. $this->count++;
  365. }
  366. }
  367. return $arr;
  368. }
  369. /**
  370. 读取全部同时将第一个字段作为主键(读取的数据存在数组里)
  371. */
  372. public function getkeyall($table,$fields,$where='')
  373. {
  374. $sql = $this->getsql(array(
  375. 'table' => $table,
  376. 'where' => $where,
  377. 'fields'=> $fields
  378. ));
  379. $res=$this->query($sql);
  380. $arr=array();
  381. if($res){
  382. while(list($ka,$ab) = $this->fetch_array($res, 1)){
  383. $arr[$ka]=$ab;
  384. $this->count++;
  385. }
  386. }
  387. return $arr;
  388. }
  389. /**
  390. 读取一条sql语句用规定字符连接起来
  391. */
  392. public function getjoinval($table,$fields,$where='',$join=',')
  393. {
  394. $sql = $this->getsql(array(
  395. 'table' => $table,
  396. 'where' => $where,
  397. 'fields'=> $fields
  398. ));
  399. $res=$this->query($sql);
  400. $arr=array();
  401. if($res){
  402. while(list($kv) = $this->fetch_array($res, 1)){
  403. $arr[]=$kv;
  404. $this->count++;
  405. }
  406. }
  407. return join($join,$arr);
  408. }
  409. /**
  410. 读取某行某字段的
  411. */
  412. public function getmou($table,$fields,$where,$order='')
  413. {
  414. $sql = $this->getsql(array(
  415. 'table' => $table,
  416. 'where' => $where,
  417. 'fields'=> $fields,
  418. 'order' => $order
  419. ));
  420. $res=$this->query($sql);
  421. if($res){
  422. $row = $this->fetch_array($res, 1);
  423. if($row){
  424. $this->count = 1;
  425. return $row[0];
  426. }
  427. }
  428. return false;
  429. }
  430. /**
  431. * 开启事务
  432. */
  433. public function routinestart()
  434. {
  435. $this->starttran();
  436. }
  437. /**
  438. * 提交/回滚事务
  439. * $bo=null 自动 true 提交,false 回滚
  440. */
  441. public function routineend($bo=null)
  442. {
  443. if(!is_bool($bo))$bo = $this->backsql();
  444. $this->endtran($bo);
  445. return $bo;
  446. }
  447. /**
  448. * 启用事务,没有事务
  449. */
  450. private function tranbegin($sql)
  451. {
  452. //if($this->errorbool)return false;
  453. if($this->conn == null)$this->connect();
  454. $this->iudcount++;
  455. if(!$this->tran){
  456. //$this->starttran();
  457. //$this->tran=true;
  458. }
  459. $rsa = $this->query($sql);
  460. $this->iudarr[]=$rsa;
  461. if(!$rsa)$this->errorbool = true;
  462. return $rsa;
  463. }
  464. /**
  465. 事务结束
  466. */
  467. private function tranend()
  468. {
  469. if($this->tran){
  470. //$this->endtran($this->backsql());
  471. }
  472. $this->tran=false;
  473. }
  474. /**
  475. 判断插入更新删除sql语句是否有错
  476. */
  477. public function backsql()
  478. {
  479. $subt=true;
  480. foreach($this->iudarr as $tra){
  481. if(!$tra){
  482. $subt=false;
  483. break;
  484. }
  485. }
  486. return $subt;
  487. }
  488. public function insert($table,$name,$values,$sel=false)
  489. {
  490. $sql="insert into ".$this->gettables($table)." ($name) ";
  491. if(!$sel){
  492. $sql.="values($values)";
  493. }else{
  494. $sql.=$values;
  495. }
  496. return $this->tranbegin($sql);
  497. }
  498. public function update($table,$content,$where)
  499. {
  500. $where = $this->getwhere($where);
  501. $sql="update ".$this->gettables($table)." set $content where $where ";
  502. return $this->tranbegin($sql);
  503. }
  504. public function delete($table,$where)
  505. {
  506. $where = $this->getwhere($where);
  507. $sql="delete from ".$this->gettables($table)." where $where ";
  508. return $this->tranbegin($sql);
  509. }
  510. /**
  511. 记录添加修改
  512. */
  513. public function record($table,$array,$where='')
  514. {
  515. $addbool = true;
  516. if(!$this->isempt($where))$addbool=false;
  517. $cont = '';
  518. if(is_array($array)){
  519. foreach($array as $key=>$val){
  520. $cont.=",`$key`=".$this->toaddval($val)."";
  521. }
  522. $cont = substr($cont,1);
  523. }else{
  524. $cont = $array;
  525. }
  526. $table = $this->gettables($table);
  527. if($addbool){
  528. $sql="insert into $table set $cont";
  529. }else{
  530. $where = $this->getwhere($where);
  531. $sql="update $table set $cont where $where";
  532. }
  533. return $this->tranbegin($sql);
  534. }
  535. /**
  536. * 处理表名
  537. */
  538. public function gettables($str)
  539. {
  540. if(!contain($str,'`'))$str='`'.$str.'`';
  541. return $str;
  542. }
  543. /**
  544. 返回总条数
  545. */
  546. public function rows($table,$where,$rowtype='count(1)'){
  547. return (int)$this->getmou($table,$rowtype,$where);
  548. }
  549. /**
  550. 返回所有数据库的表
  551. */
  552. public function getalltable($base='')
  553. {
  554. if($base=='')$base = $this->basename;
  555. $sql = "select `TABLE_NAME` from information_schema.`TABLES` where `TABLE_SCHEMA`='$base'";
  556. $arr = $this->getall($sql);
  557. $rows= array();
  558. foreach($arr as $k=>$rs)$rows[] = $rs['TABLE_NAME'];
  559. return $rows;
  560. }
  561. /**
  562. * 返回表所有字段,如['id','name']
  563. */
  564. public function getallfields($table)
  565. {
  566. $sql = 'SHOW FULL COLUMNS FROM `'.$table.'`';
  567. return $this->getall($sql, function($row){
  568. return $row['Field'];
  569. });
  570. }
  571. public function getfields($table)
  572. {
  573. $f = $this->getallfields($table);
  574. foreach($f as $f1)$arr[$f1]='';
  575. return $arr;
  576. }
  577. public function gettablefields($table, $base='',$whe='')
  578. {
  579. if($base=='')$base = $this->db_base;
  580. $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`";
  581. if($whe)return $this->getall($sql);
  582. $sql = 'SHOW FULL COLUMNS FROM '.$base.'.`'.$table.'`;';
  583. return $this->getall($sql, function($row){
  584. $len = null;$dbtype = strtolower($row['Type']);
  585. $arrs = explode('(',$dbtype);
  586. $type = $arrs[0];
  587. if(isset($arrs[1]))$len = (int)str_replace(')','', $arrs[1]);
  588. return array(
  589. 'name' => $row['Field'],
  590. 'types' => $row['Type'],
  591. 'explain' => $row['Comment'],
  592. 'type' => $type,
  593. 'dev' => $row['Default'],
  594. 'isnull' => $row['Null'],
  595. 'lens' => $len,
  596. 'xslen1' => 0,
  597. 'xslen2' => 0,
  598. );
  599. });
  600. }
  601. /**
  602. 读取表结构
  603. */
  604. public function gettablecolumn($table, $fields='')
  605. {
  606. $where = '';
  607. if($fields!='')$where = "and `COLUMN_NAME`='$fields'";
  608. $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`";
  609. $arr = $this->getall($sql);
  610. $rows = array();
  611. foreach($arr as $k=>$rs){
  612. $dev = 'NULL';
  613. if(!$this->isempt($rs['defval']))$dev=$rs['defval'];
  614. $str = "`".$rs['name']."` ".$rs['types']." DEFAULT ".$dev."";
  615. if(!$this->isempt($rs['explain']))$str.=" COMMENT '".$rs['explain']."'";
  616. $rows[] = $str;
  617. }
  618. return $rows;
  619. }
  620. public function showcreatetable($table)
  621. {
  622. $sql = "show create table `$table`";
  623. $res= $this->query($sql);
  624. list($ka,$nr) = $this->fetch_array($res, 1);
  625. return $nr;
  626. }
  627. /**
  628. 判断变量是否为空
  629. */
  630. public function isempt($str)
  631. {
  632. return isempt($str);
  633. }
  634. public function contain($str,$a)
  635. {
  636. $bool=false;
  637. if(!$this->isempt($a) && !$this->isempt($str)){
  638. $ad=strpos($str,$a);
  639. if($ad>0||!is_bool($ad))$bool=true;
  640. }
  641. return $bool;
  642. }
  643. /**
  644. 转换数据库可插入的对象
  645. */
  646. public function toaddval($str)
  647. {
  648. $adstr="'$str'";
  649. if($this->isempt($str)){
  650. $adstr='null';
  651. }else{
  652. if(substr($str,0,4)=='(&;)')$adstr=substr($str,4);
  653. }
  654. return $adstr;
  655. }
  656. /**
  657. * 替换特殊符合'
  658. */
  659. public function tocovexec($str, $lx=0)
  660. {
  661. $str = str_replace('\'', '&#39;',$str);
  662. if($lx==1){
  663. $str = str_replace("\n", '',$str);
  664. }
  665. return $str;
  666. }
  667. /**
  668. 创建随机编号
  669. */
  670. public function ranknum($table,$field='num',$n=6, $dx=0)
  671. {
  672. $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');
  673. $num = '';
  674. for($i=1;$i<=$n;$i++)$num.=$arr[rand(0,count($arr)-1)];
  675. if($dx==1)$num = strtoupper($num);//转换成大写
  676. $rsnum = $this->getmou($table,$field,"`$field`='$num'");
  677. return ($rsnum)?$this->ranknum($table,$field,$n, $dx):$num;
  678. }
  679. /**
  680. 流水编号
  681. */
  682. public function sericnum($num, $table,$fields='sericnum', $ws=4, $whe='')
  683. {
  684. $dts = explode('-', date('Y-m-d'));
  685. $ymd = $dts[0].$dts[1].$dts[2];
  686. $ym = $dts[0].$dts[1];
  687. $num = str_replace('Ymd', $ymd, $num);
  688. $num = str_replace('Ym', $ym, $num);
  689. $num = str_replace('Year', $dts[0], $num);
  690. $num = str_replace('Day', $dts[2], $num);
  691. $num = str_replace('Month', $dts[1], $num);
  692. $where = "`$fields` like '".$num."%' $whe";
  693. $max = (int)$this->getmou($table, "max(cast(replace(`$fields`,'$num','') as decimal(10)))", $where);
  694. $max++;
  695. $wsnum = ''.$max.'';
  696. $len = strlen($wsnum);
  697. $oix = $ws - $len;
  698. for($i=1;$i<=$oix;$i++)$wsnum='0'.$wsnum;
  699. $num .= $wsnum;
  700. return $num;
  701. }
  702. /**
  703. * 获取所有顶级信息连接起来
  704. * @param $table 表名
  705. * @param $pfields 上级字段 $jfield 要连接的字段名 $afid = 值
  706. */
  707. private $joinarr=array();
  708. private $joinlen;
  709. public function getpval($table,$pfields,$jfield,$afid,$plit='/',$afield='id',$maxlen=8)
  710. {
  711. $this->joinarr = array();
  712. $this->joinlen = 0;
  713. $this->getpvala($table,$pfields,$jfield,$afid,$afield,$maxlen);
  714. return join($plit,array_reverse($this->joinarr));
  715. }
  716. private function getpvala($table,$pfields,$jfield,$afid,$afield,$maxlen)
  717. {
  718. if(count($this->joinarr)>=$maxlen)return;
  719. $rsa = $this->getone($table,"`$afield`='$afid'","`id`,`$pfields`,`$jfield`");
  720. if($rsa){
  721. $this->joinarr[]=$rsa[$jfield];
  722. $pid = $rsa[$pfields];
  723. if($pid!=$afid)if($this->rows($table,"`$afield`='$pid'")>0)$this->getpvala($table,$pfields,$jfield,$pid,$afield,$maxlen);
  724. }
  725. }
  726. /**
  727. * 添加字段
  728. */
  729. public function addFields($table, $fields, $types, $dev=null, $name='')
  730. {
  731. $sql = "ALTER TABLE `$table` ADD `$fields` ".$types."";
  732. if($dev===null){
  733. $sql.=' DEFAULT NULL';
  734. }else if(!isempt($dev)){
  735. $sql.=" DEFAULT '$dev'";
  736. }
  737. if(!isempt($name))$sql.=" COMMENT '$name'";
  738. return $this->query($sql);
  739. }
  740. /**
  741. * 编辑字段
  742. */
  743. public function editFields($table, $fields, $types, $dev=null, $name='')
  744. {
  745. $sql = "ALTER TABLE `$table` MODIFY `$fields` ".$types."";
  746. if($dev===null){
  747. $sql.=' DEFAULT NULL';
  748. }else if(!isempt($dev)){
  749. $sql.=" DEFAULT '$dev'";
  750. }
  751. if(!isempt($name))$sql.=" COMMENT '$name'";
  752. return $this->query($sql);
  753. }
  754. }
  755. class DB{
  756. public static $tablename;
  757. public static function table($tab)
  758. {
  759. self::$tablename = ''.getconfig('.perfix.').$tab.'';
  760. return m($tab);
  761. }
  762. public static function where($f, $v)
  763. {
  764. }
  765. }
粤ICP备19079148号