一个构造sql语句的类

php中文网
发布: 2016-07-25 09:11:12
原创
1182人浏览过
  1. /**
  2. * @package Database Class
  3. * @author injection (mail:injection.mail@gmail.com)
  4. * @version 1.0
  5. */
  6. @ini_set( 'display_errors',0 );
  7. class DataBase{
  8. private $mDb_host,$mAb_user,$mAb_pwd,$mConn_No;
  9. function DataBase( $Conn_Obj ){
  10. $this->connectDb( $Conn_Obj );
  11. }
  12. function connectDb( $Conn_Obj ){
  13. $this->mDb_host = $Conn_Obj->host;
  14. $this->mAd_name = $Conn_Obj->user;
  15. $this->mAd_pwd = $Conn_Obj->pwd;
  16. $this->mConn_No = mysql_connect( $this->mDb_host, $this->mAd_name, $this->mAd_pwd );
  17. }
  18. function selectDb( $Conn_Obj ){
  19. $this->mDb_name = $Conn_Obj->dbname;
  20. mysql_select_db( $this->mDb_name );
  21. }
  22. }
  23. /**
  24. * @package Making Sqls Class exetends Database Class
  25. * @author injection (mail:injection.mail@gmail.com)
  26. * @version 1.0
  27. */
  28. class MakeSql extends DataBase{
  29. private $mSql;
  30. function MakeSql( $type,$arr_colum_list, $arr_sql_choice ){
  31. $this->MakeSqlType( $arr_colum_list, $arr_sql_choice );
  32. }
  33. #switch make list
  34. function MakeSqlType( $type, $arr_colum_list, $arr_sql_choice ){
  35. switch( $type ){
  36. case 'insert':
  37. return $this->makeInsert( $arr_colum_list, $arr_sql_choice );
  38. case 'select':
  39. return $this->makeSelect( $arr_colum_list, $arr_sql_choice );
  40. case 'update':
  41. return $this->makeUpdate( $arr_colum_list, $arr_sql_choice );
  42. case 'delete':
  43. return $this->makeDelete( $arr_colum_list, $arr_sql_choice );
  44. }
  45. }
  46. #make insert
  47. function makeInsert( $arr_colum_list,$arr_sql_choice ){
  48. $colum_key = array_keys( $arr_colum_list );
  49. $colum_value = array_values( $arr_colum_list );
  50. $this->mSql = "INSERT INTO ".$arr_sql_choice["tbl_name"]."( ".join( ',' , $colum_key )." ) VALUES( '".join( "','" , $colum_value )."')";
  51. return $this->mSql;
  52. }
  53. #making select
  54. function makeSelect( $arr_colum_list = '*' , $arr_sql_choice ){
  55. $colum_value = array_keys( $arr_colum_list );
  56. foreach( $arr_sql_choice as $sql_key => $sql_value ){
  57. if( strcmp( $sql_key, 'tbl_name' ) == 0 ){
  58. if( strcmp($arr_colum_list, '*' ) !== 0 )
  59. $this->mSql = "SELECT ".join( ',' , $colum_value )." FROM ".$sql_value;
  60. else
  61. $this->mSql = "SELECT * FROM ".$sql_value;
  62. }
  63. else
  64. if( strcmp( $sql_value, '' ) !== 0 )
  65. if(strcmp( $sql_key, 'WHERE' ) === 0 && strcmp( $sql_value, 'colum' ) === 0 ){
  66. foreach($arr_colum_list As $colum_key => $colum_value )
  67. $this->mSql .= "$colum_key = '$colum_value' AND ";
  68. $this->mSql = rtrim( $this->mSql, " AND " );
  69. }
  70. else
  71. $this->mSql .= " $sql_key ".$sql_value;
  72. }
  73. return $this->mSql;
  74. }
  75. #making update
  76. function makeUpdate( $arr_colum_list, $arr_sql_choice ){
  77. $this->mSql = "UPDATE ".$arr_sql_choice['tbl_name']." SET ";
  78. foreach( $arr_colum_list as $colum_key => $colum_value )
  79. $this->mSql .= "$colum_key = '$colum_value',";
  80. $this->mSql = rtrim( $this->mSql , ',');
  81. foreach( $arr_sql_choice as $sql_key => $sql_value ){
  82. if( strcmp( $sql_value, '' ) !== 0 && strcmp( $sql_key, 'tbl_name' ) !==0 && strcmp( $sql_key, 'ORDER BY' ) !== 0 )
  83. $this->mSql .= " $sql_key ".$sql_value;
  84. }
  85. return $this->mSql;
  86. }
  87. #making delete
  88. function makeDelete( $arr_colum_list, $arr_sql_choice ){
  89. $this->mSql = "DELETE FROM ".$arr_sql_choice['tbl_name'];
  90. foreach( $arr_sql_choice as $sql_key => $sql_value ){
  91. if( strcmp( $sql_key, 'tbl_name' ) !== 0 && strcmp( $sql_value, '' ) !== 0 ){
  92. $this->mSql .= " $sql_key ".$sql_value;
  93. }
  94. }
  95. return $this->mSql;
  96. }
  97. }
复制代码


最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号