db_mysqli.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  1. <?php
  2. define('InEmpireCMSDbSql',TRUE);
  3. //------------------------- 数据库 -------------------------
  4. //链接数据库
  5. function do_dbconnect($dbhost,$dbport,$dbusername,$dbpassword,$dbname){
  6. global $ecms_config;
  7. $dblocalhost=$dbhost;
  8. //端口
  9. if($dbport)
  10. {
  11. $dblocalhost.=':'.$dbport;
  12. }
  13. $dblink=@mysqli_connect($dblocalhost,$dbusername,$dbpassword);
  14. if(!$dblink)
  15. {
  16. echo"Cann't connect to DB!";
  17. exit();
  18. }
  19. //编码
  20. if($ecms_config['db']['dbver']>='4.1')
  21. {
  22. $q='';
  23. if($ecms_config['db']['setchar'])
  24. {
  25. $q='character_set_connection='.$ecms_config['db']['setchar'].',character_set_results='.$ecms_config['db']['setchar'].',character_set_client=binary';
  26. }
  27. if($ecms_config['db']['dbver']>='5.0')
  28. {
  29. $q.=(empty($q)?'':',').'sql_mode=\'\'';
  30. }
  31. if($q)
  32. {
  33. @mysqli_query($dblink,'SET '.$q);
  34. }
  35. }
  36. @mysqli_select_db($dblink,$dbname);
  37. return $dblink;
  38. }
  39. //关闭数据库
  40. function do_dbclose($dblink){
  41. if($dblink)
  42. {
  43. @mysqli_close($dblink);
  44. }
  45. }
  46. //设置编码
  47. function do_DoSetDbChar($dbchar,$dblink){
  48. @mysqli_query($dblink,'set character_set_connection='.$dbchar.',character_set_results='.$dbchar.',character_set_client=binary;');
  49. }
  50. //取得mysql版本
  51. function do_eGetDBVer($selectdb=0){
  52. global $empire,$link;
  53. if($selectdb&&$empire)
  54. {
  55. $getdbver=$empire->egetdbver();
  56. }
  57. else
  58. {
  59. if($link)
  60. {
  61. $getdbver=@mysqli_get_server_info($link);
  62. }
  63. else
  64. {
  65. $getdbver='';
  66. }
  67. }
  68. return $getdbver;
  69. }
  70. //普通操作
  71. function do_dbconnect_common($dbhost,$dbport,$dbusername,$dbpassword,$dbname=''){
  72. global $ecms_config;
  73. $dblocalhost=$dbhost;
  74. //端口
  75. if($dbport)
  76. {
  77. $dblocalhost.=':'.$dbport;
  78. }
  79. $dblink=@mysqli_connect($dblocalhost,$dbusername,$dbpassword);
  80. return $dblink;
  81. }
  82. function do_dbquery_common($query,$dblink,$ecms=0){
  83. global $ecms_config;
  84. if($ecms==0)
  85. {
  86. $sql=mysqli_query($dblink,$query);
  87. }
  88. else
  89. {
  90. $sql=mysqli_query($dblink,$query) or die($ecms_config['db']['showerror']==1?str_replace($GLOBALS['dbtbpre'],'***_',mysqli_error($dblink).'<br>'.$query):'DbError');
  91. }
  92. return $sql;
  93. }
  94. function do_dbfetch_common($sql){
  95. $r=mysqli_fetch_array($sql);
  96. return $r;
  97. }
  98. function do_dblastid_common($dblink){
  99. $id=mysqli_insert_id($dblink);
  100. if($id<0)
  101. {
  102. $sql=do_dbquery_common('SELECT last_insert_id() as total',$dblink);
  103. $r=do_dbfetch_common($sql);
  104. $id=$r['total'];
  105. }
  106. return $id;
  107. }
  108. //选择数据库
  109. function do_eUseDb($dbname,$dblink,$query=0){
  110. if($query)
  111. {
  112. $usedb=do_dbquery_common('use `'.$dbname.'`',$dblink);
  113. }
  114. else
  115. {
  116. $usedb=@mysqli_select_db($dblink,$dbname);
  117. }
  118. return $usedb;
  119. }
  120. //------------------------- 数据库操作 -------------------------
  121. class mysqlquery
  122. {
  123. var $dblink;
  124. var $sql;//sql语句执行结果
  125. var $query;//sql语句
  126. var $num;//返回记录数
  127. var $r;//返回数组
  128. var $id;//返回数据库id号
  129. //执行mysql_query()语句
  130. function query($query){
  131. global $ecms_config;
  132. $this->sql=mysqli_query(return_dblink($query),$query) or die($ecms_config['db']['showerror']==1?str_replace($GLOBALS['dbtbpre'],'***_',mysqli_error(return_dblink($query)).'<br>'.$query):'DbError');
  133. return $this->sql;
  134. }
  135. //执行mysql_query()语句2
  136. function query1($query){
  137. $this->sql=mysqli_query(return_dblink($query),$query);
  138. return $this->sql;
  139. }
  140. //执行mysql_query()语句(选择数据库USE)
  141. function usequery($query){
  142. global $ecms_config;
  143. $this->sql=mysqli_query($GLOBALS['link'],$query) or die($ecms_config['db']['showerror']==1?str_replace($GLOBALS['dbtbpre'],'***_',mysqli_error($GLOBALS['link']).'<br>'.$query):'DbError');
  144. if($GLOBALS['linkrd'])
  145. {
  146. mysqli_query($GLOBALS['linkrd'],$query);
  147. }
  148. return $this->sql;
  149. }
  150. //执行mysql_query()语句(操作数据库)
  151. function updatesql($query){
  152. global $ecms_config;
  153. $this->sql=mysqli_query(return_dblink($query),$query) or die($ecms_config['db']['showerror']==1?str_replace($GLOBALS['dbtbpre'],'***_',mysqli_error(return_dblink($query)).'<br>'.$query):'DbError');
  154. return $this->sql;
  155. }
  156. //执行mysql_fetch_array()
  157. function fetch($sql)//此方法的参数是$sql就是sql语句执行结果
  158. {
  159. $this->r=mysqli_fetch_array($sql);
  160. return $this->r;
  161. }
  162. //执行fetchone(mysql_fetch_array())
  163. //此方法与fetch()的区别是:1、此方法的参数是$query就是sql语句
  164. //2、此方法用于while(),for()数据库指针不会自动下移,而fetch()可以自动下移。
  165. function fetch1($query)
  166. {
  167. $this->sql=$this->query($query);
  168. $this->r=mysqli_fetch_array($this->sql);
  169. return $this->r;
  170. }
  171. //执行mysql_num_rows()
  172. function num($query)//此类的参数是$query就是sql语句
  173. {
  174. $this->sql=$this->query($query);
  175. $this->num=mysqli_num_rows($this->sql);
  176. return $this->num;
  177. }
  178. //执行numone(mysql_num_rows())
  179. //此方法与num()的区别是:1、此方法的参数是$sql就是sql语句的执行结果。
  180. function num1($sql)
  181. {
  182. $this->num=mysqli_num_rows($sql);
  183. return $this->num;
  184. }
  185. //执行numone(mysql_num_rows())
  186. //统计记录数
  187. function gettotal($query)
  188. {
  189. $this->r=$this->fetch1($query);
  190. return $this->r['total'];
  191. }
  192. //执行free(mysql_result_free())
  193. //此方法的参数是$sql就是sql语句的执行结果。只有在用到mysql_fetch_array的情况下用
  194. function free($sql)
  195. {
  196. mysqli_free_result($sql);
  197. }
  198. //执行seek(mysql_data_seek())
  199. //此方法的参数是$sql就是sql语句的执行结果,$pit为执行指针的偏移数
  200. function seek($sql,$pit)
  201. {
  202. mysqli_data_seek($sql,$pit);
  203. }
  204. //执行id(mysql_insert_id())
  205. function lastid()//取得最后一次执行mysql数据库id号
  206. {
  207. $this->id=mysqli_insert_id($GLOBALS['link']);
  208. if($this->id<0)
  209. {
  210. $this->id=$this->gettotal('SELECT last_insert_id() as total');
  211. }
  212. return $this->id;
  213. }
  214. //返回影响数量(mysql_affected_rows())
  215. function affectnum()//取得操作数据表后受影响的记录数
  216. {
  217. return mysqli_affected_rows($GLOBALS['link']);
  218. }
  219. //执行escape_string()函数
  220. function EDbEscapeStr($str){
  221. $str=mysqli_real_escape_string($GLOBALS['link'],$str);
  222. return $str;
  223. }
  224. //取得数据库版本
  225. function egetdbver()
  226. {
  227. $this->r=$this->fetch1('select version() as version');
  228. return $this->r['version'];
  229. }
  230. }
  231. ?>