db_mysql.php 5.8 KB

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