Home > Backend Development > PHP Tutorial > mysql operation class, v0.2, adding some solutions to deal with large data volumes

mysql operation class, v0.2, adding some solutions to deal with large data volumes

WBOY
Release: 2016-07-25 09:06:19
Original
1049 people have browsed it
/*mysql simple class by joffe q89949401 bib@Crazycode poet; This class is completely static. When using it, include it directly and use the mysql::method() name. Since the class is globally visible in php5, there is no need to worry about the variable scope. If you have any comments, please send a private message ||qq mail; Currently there is no method related to stored procedures. Of course, stored procedures are usually done when creating a database. The config file needs to configure the following constant information: LIB: class storage location DEBUG: Whether to enable debug (if enabled, error information tracking will be output) TB_EX: database table prefix;

Added select del update insert big_select big_del function

*/
  1. /*mysql simple class by joffe q89949401 bib@Crazycode poet;
  2. When this class is used completely statically, include it directly and use the mysql::method() name because the class is global in php5 Visible, so you don’t have to worry about variable scope. If you have any comments, please send a private message ||qq mail;
  3. There is currently no method related to stored procedures. Of course, stored procedures are generally done when creating a database.
  4. The config file needs to be configured The following constant information:
  5. LIB: Class storage location
  6. DEBUG: Whether to enable debug (if it is enabled, error message tracking will be output)
  7. TB_EX: Database table prefix;
  8. */
  9. defined('LIB') or die('Missing config!! ');
  10. final class mysql {
  11. /**
  12. * Total number of queries
  13. *
  14. * @var int
  15. */
  16. public static $querynum = 0;
  17. /**
  18. * connection handle
  19. *
  20. * @var object
  21. */
  22. public static $link;
  23. /*
  24. table prefix
  25. @var string The following method requires TB_EX to be configured in the configuration file as the prefix of the table
  26. */
  27. static function add_ex($tb){
  28. return TB_EX.$tb_ex.$tb;
  29. }
  30. /*Whether the mysql database uses strict types (mysql The type is not automatically converted) The default is false, which means that mysql has type conversion turned on. This variable is currently used as a single quote for the insert function in mysql without automatic conversion. Related functions may be added in the future
  31. */
  32. public static $is_type_tight=false;
  33. /**
  34. * Constructor
  35. *
  36. * @param string $dbhost host name
  37. * @param string $dbuser user
  38. * @param string $dbpw password
  39. * @param string $dbname database name
  40. * @param int $pconnect whether to persist the connection
  41. */
  42. static function connent($dbhost, $dbuser, $dbpw, $dbname = "",$dbcharset, $pconnect = 0) {
  43. if($ pconnect) {
  44. if(!self::$link = @mysql_pconnect($dbhost, $dbuser, $dbpw)) {
  45. self::halt("Can not connect to MySQL server");
  46. }
  47. } else {
  48. if(!self::$link = @mysql_connect($dbhost, $dbuser, $dbpw)) {
  49. self::halt("Can not connect to MySQL server");
  50. }
  51. }
  52. if(self::version () > "4.1") {
  53. if($dbcharset) {
  54. mysql_query("SET character_set_connection={$dbcharset}, character_set_results=$dbcharset, character_set_client=binary", self::$link);
  55. }
  56. if( self::version() > "5.0.1") {
  57. mysql_query("SET sql_mode=''", self::$link);
  58. }
  59. }
  60. if($dbname) {
  61. mysql_select_db($dbname, self::$link);
  62. }
  63. }
  64. /**
  65. * Select database
  66. *
  67. * @param string $dbname
  68. * @return
  69. */
  70. static function select_db($dbname) {
  71. return mysql_select_db($dbname, self::$link);
  72. }
  73. /**
  74. * Get a record from the result set
  75. *
  76. * @param object $query
  77. * @param int $result_type
  78. * @return array
  79. */
  80. static function fetch_array($query, $result_type = MYSQL_ASSOC) { //By default, only associative arrays are fetched, not numeric arrays.
  81. return mysql_fetch_array($query, $result_type);
  82. }
  83. /**
  84. * 查询SQL
  85. *
  86. * @param string $sql
  87. * @param string $type
  88. * @return object
  89. */
  90. static function query($sql, $type = "") {
  91. $func = $type == "UNBUFFERED" && @function_exists("mysql_unbuffered_query") ?
  92. "mysql_unbuffered_query" : "mysql_query";
  93. if( !($query = $func($sql, self::$link)) && $type != "SILENT") {
  94. self::halt("MySQL Query Error", $sql);
  95. }
  96. self:: $querynum++;
  97. return $query;
  98. }
  99. /**
  100. * Get the number of affected items
  101. *
  102. * @return int
  103. */
  104. static function affected_rows() {
  105. return mysql_affected_rows(self::$link);
  106. }
  107. /**
  108. * Return error message
  109. *
  110. * @return array
  111. */
  112. static function error() {
  113. return ((self::$link) ? mysql_error(self::$link) : mysql_error());
  114. }
  115. /**
  116. * Return error code
  117. *
  118. * @return int
  119. */
  120. static function errno() {
  121. return intval((self::$link) ? mysql_errno(self::$link) : mysql_errno());
  122. }
  123. /**
  124. * Return query results
  125. *
  126. * @param object $query
  127. * @param string $row
  128. * @return mixed
  129. */
  130. static function result($query, $row,$flname=0 ) {
  131. $query = @mysql_result($query, $row,$flname);
  132. return $query;
  133. }
  134. /**
  135. * Number of results
  136. *
  137. * @param object $query
  138. * @return int
  139. */
  140. static function num_rows($query) {
  141. $query = mysql_num_rows( $query);
  142. return $query;
  143. }
  144. /**
  145. * Get the total number of fields
  146. *
  147. * @param object $query
  148. * @return int
  149. */
  150. static function num_fields($query) {
  151. return mysql_num_fields($query);
  152. }
  153. /**
  154. * Release the result set
  155. *
  156. * @param object $query
  157. * @return bool
  158. */
  159. static function free_result($query) {
  160. return @mysql_free_result($query);
  161. }
  162. /**
  163. * Return auto-increment ID
  164. *
  165. * @return int
  166. */
  167. static function insert_id() {
  168. return ($id = mysql_insert_id(self::$link)) >= 0 ? $id : self::$result(self::$query("SELECT last_insert_id()"), 0);
  169. }
  170. /**
  171. * Get a row from the result set as an enumeration array
  172. *
  173. * @param object $query
  174. * @return array
  175. */
  176. static function fetch_row($query) {
  177. $query = mysql_fetch_row($query);
  178. return $query;
  179. }
  180. /**
  181. * Get column information from the result set and return it as an object
  182. *
  183. * @param object $query
  184. * @return object
  185. */
  186. static function fetch_fields($query) {
  187. return mysql_fetch_field($query);
  188. }
  189. static function select_affectedt_rows($rs){
  190. return mysql_affected_rows($rs,self::$link);
  191. }
  192. /**
  193. * Return mysql version
  194. *
  195. * @return string
  196. */
  197. static function version() {
  198. return mysql_get_server_info(self::$link);
  199. }
  200. /**
  201. * Close connection
  202. *
  203. * @return bool
  204. */
  205. static function close() {
  206. return mysql_close(self::$link);
  207. }
  208. /**
  209. * Output error message
  210. *
  211. * @param string $message
  212. * @param string $sql
  213. */
  214. static function halt($message = "", $sql = "") {
  215. @header("Content-type: text/html; charset=utf-8");
  216. if (DEBUG==1){
  217. $debug = debug_backtrace();
  218. echo $message . "rn
    SQL--> " . $sql."rn
    ERROR_MESSAGE-->".self::error().
  219. "rn
    --------------debug--------------rn
    ";
  220. self::echoarray($debug);
  221. echo "rn
    -------------debug end----------------";
  222. }else{
  223. echo 'SQL Error';
  224. }
  225. @self::rollback();
  226. exit;
  227. }
  228. /////////////////////////////以下是扩展的sql方法.//////
  229. /* 把数组按照 key value value 的对应关系插入数据表table中
  230. table 要插入的数据表
  231. 要注意 这些扩展方法是没自己给表有加前缀的.
  232. */
  233. static function insert($table,$array){
  234. $temp="";$temp2='';
  235. foreach($array as $key=>$value){
  236. if(self::$is_type_tight){
  237. if(is_string($value)){
  238. $temp .="$key,";$temp2 .="'$value',";
  239. }elseif(is_int($value||is_null($value)||is_float($value))){
  240. $value+=0;
  241. $temp .="$key,";$temp2 .="'$value',";
  242. }
  243. }else{
  244. $temp .="$key,";$temp2 .="'$value',";
  245. }
  246. }
  247. $temp = substr($temp,0,strlen($temp)-1);
  248. $temp2 = substr($temp2,0,strlen($temp2)-1);
  249. $sql = "INSERT INTO $table ($temp) VALUE($temp2)";
  250. return self::query($sql);
  251. }
  252. static function del($table,$where){
  253. $sql = "DELETE FROM {$table} where {$where}";
  254. return self::query($sql);
  255. }
  256. static function update($table,$array,$where){
  257. foreach ($array as $key=>$value){
  258. $temp .= "$key='$value',";
  259. }
  260. $temp = substr($temp,0,strlen($temp)-1);
  261. $sql = "update {$table} set ($temp) where {$where} ";
  262. return self::query($sql);
  263. }
  264. /*进行数据库查询select 参数不定
  265. 参数说明:所有参数必须是string
  266. 第一个参数必须是表名;
  267. 从第二个参数起,
  268. 如果是写上 "where:XXX" 则认为是where条件;
  269. 如果写上 "xxx" 则认为是键值
  270. 如果写上 "by:XXX" 则认为是排序
  271. 如果写上 "limit:xxx,xxx" 则认为是分页
  272. #参数不正确则返回false; 成功查询返回查询后的数组;
  273. */
  274. static function select(){
  275. $numargs = func_num_args();//获取参数个数;
  276. $where = "";$key="";$limit="";$by="";
  277. if($numargs==0){return false;}
  278. //echo $numargs;
  279. if($numargs>=2){
  280. $arg_list = func_get_args();
  281. $table = $arg_list[0];
  282. unset($arg_list[0]);
  283. // print_r($arg_list);
  284. foreach($arg_list as $k=>$value){
  285. if(preg_match("#^(where:)w#",$value)){
  286. $temp = explode(":",$value);
  287. $where = "WHERE {$temp[1]} " ;
  288. }elseif(preg_match("#^by:w#",$value)){
  289. $temp = explode(":",$value);
  290. $by = "order by {$temp[1]}" ;
  291. }elseif(preg_match("#^limit:w#",$value)){
  292. $temp = explode(":",$value);
  293. $limit = "limit {$temp[1]}";
  294. }else{
  295. $key .= "$value,";
  296. }
  297. }
  298. if($key==""){
  299. $key = "*";
  300. }else{
  301. $key =substr($key,0,strlen($key)-1);
  302. }
  303. $sql_base="SELECT $key FROM $table";
  304. }
  305. if(!empty($where)){
  306. $sql_base .= " $where";
  307. }
  308. if(!empty($by)){
  309. $sql_base .= " $by";
  310. }
  311. if(!empty($limit)){
  312. $sql_base .= " $limit";
  313. }
  314. //echo $sql_base;
  315. //echo $by ;
  316. $rs = self::query($sql_base);
  317. $re=array();
  318. if(self::num_rows($rs)>=1){
  319. while($info = self::fetch_array($rs)){
  320. $re[]=$info;
  321. }
  322. }
  323. self::free_result($rs);
  324. return $re;
  325. }
  326. /*回滚事务*/
  327. static function rollback(){
  328. self::query('rollback');
  329. }
  330. /*开始事务*/
  331. static function begin(){
  332. self::query('SET AUTOCOMMIT =0'); //停用自动提交;
  333. self::query('BEGIN') ;//开始一个事务;
  334. }
  335. /*提交事务*/
  336. static function commit(){
  337. self::query('commit');
  338. }
  339. static function echoarray($array){
  340. foreach($array as $k=>$v ){
  341. if(is_array($v)){
  342. if(is_array($v)){
  343. echo "
    --------------------------------
    ";
  344. self::echoarray($v);
  345. }
  346. }else{
  347. if($k==='line')
  348. echo "$k -> " .$v." ";
  349. else
  350. echo "$k -> " .$v." ";
  351. }
  352. }
  353. }
  354. function get_server_info(){
  355. return mysql_get_server_info();
  356. }
  357. //下面是应付大数据的表的优化查询
  358. /*
  359. big_select 适合用于大规模的查询,利用覆盖索引实现大幅度的偏移活动窗口,令查询在覆盖索引上偏移而不是在所有数据上,减少msql在数据上检查,再把其他数据join进来这样更效率.但对于小规模数据,这种查询反而增加复杂度,增加优化器压力.说个例子,如果你是limit 10000,20;mysql会先查出10020条数据 再丢弃10000 这种操作代价非常大,利用这个函数可以有效提升效率,但如果是 limit 20,那就会稍微比直接select 慢了一些
  360. @table string要查询的表 如"table1"
  361. @keys string 要查询的键值,多个键值用","分割 如"key1,key2,key3"结束不含","尽量少用"*"且一些关键词请加上`;
  362. @Index string主索引键或者唯一索引键名,只需要一个 如"id";
  363. @pagesize int 分页大小,必须,你不会想这么多数据全部出来吧
  364. @pageNo 页码,从0开始
  365. @orderby string 排序 如"id desc";可空,但不建议空
  366. @where string 条件 如 "date>122424533"可空
  367. #返回数组
  368. */
  369. static function big_select($table,$keys,$index,$pagesize,$pageNo,$orderby=NULL,$where=NULL){
  370. $start=$pageNo*$pagesize;
  371. if($where){
  372. $sqlIndex="SELEECT {$index} from {$table} where {$where}";
  373. }else{
  374. $sqlIndex="SELEECT {$index} from {$table}";
  375. }
  376. if($orderby){
  377. $sqlIndex .=" ORDER BY {$orderby} Limit $start,$pagesize";
  378. }else{
  379. $sqlIndex .=" ORDER BY Limit $start,$pagesize";
  380. }
  381. $sql = "SELECT $keys FROM {$table} INNER JOIN({$sqlIndex}) AS lim USING({$index})";
  382. $rs = self::query($sql);
  383. $re=array();
  384. if(self::num_rows($rs)>=1){
  385. while($info = self::fetch_array($rs)){
  386. $re[]=$info;
  387. }
  388. }
  389. self::free_result($rs);
  390. return $re;
  391. }
  392. /* 如果一个很大的数据(预计大于万行)删除它的工作周期会比较长,会长时间锁住不应该锁住的表或行,令一些不该打断的数据被打断 以下方法是把一个大的sql任务分小(分为一次5000行)但次操作可能会造成删除空隙期间插入了新的数据,而新的数据可能因满足条件而被删除.本方法容易因为超时而失败.
  393. @table string 要删除数据的表名
  394. @where string 条件 可省略
  395. #int 删除掉的行数
  396. */
  397. static function big_del($table,$where){
  398. set_time_limit(0);
  399. $sql="delete from {$table} where {$where} Limit 5000";
  400. $rows = 0;
  401. $eff=0;
  402. do{
  403. self::query($sql);
  404. $rows=self::affected_rows();
  405. $eff += $rows;
  406. }while($rows>0);
  407. return $eff;
  408. }
  409. }
  410. ?>
复制代码


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template