DatabaseDao.class.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <?php
  2. if (!defined('IS_INITPHP')) exit('Access Denied!');
  3. class DatabaseDao {
  4. function selecti($sqlString,$resultType){
  5. $mysqliObj = new mysqli('localhost','root','123456','hzremote');
  6. if(mysqli_connect_errno()){
  7. echo "连接失败".mysqli_connect_error();
  8. exit();
  9. }
  10. $data = mysql_fetch_object($sqlString);
  11. printf($data);
  12. /*if ($result = $mysqliObj->query($sqlString)) {
  13. $row = $result->fetch_row();
  14. printf("JSON is %s!\n", $row[0]);
  15. } else {
  16. printf("Errormessage: %s\n", $mysqliObj->error());
  17. }*/
  18. //$data = $mysqliObj->query($sqlString);
  19. $mysqliObj->close();
  20. if($resultType=="entity"){
  21. return $data[0];
  22. }else if($resultType=="list") {
  23. return $data;
  24. }else if($resultType=="rows") {
  25. return $data[0]["rows"];
  26. }else if($resultType=="return"){
  27. if($data){
  28. return array("return"=>"true");
  29. }else{
  30. return false;
  31. }
  32. }else{
  33. return $data;
  34. }
  35. }
  36. //说明
  37. function select($sqlString,$resultType){
  38. $data = M()->query($sqlString);
  39. if($resultType=="entity"){
  40. return $data[0];
  41. }else if($resultType=="list") {
  42. return $data;
  43. }else if($resultType=="rows") {
  44. return $data[0]["rows"];
  45. }else if($resultType=="return"){
  46. if($data){
  47. return array("return"=>"true");
  48. }else{
  49. return false;
  50. }
  51. }else{
  52. return $data;
  53. }
  54. }
  55. function update($sqlString,$resultType){
  56. $data = M()->execute($sqlString);
  57. //if($resultType=="boolean"){
  58. if($data){
  59. return array("return"=>"true");
  60. }else{
  61. return false;
  62. }
  63. //}else{
  64. // return $data;
  65. //}
  66. }
  67. function insert ($sqlString,$resultType){
  68. if($resultType=="boolean"){
  69. $data = M()->execute($sqlString);
  70. if($data){
  71. return array("return"=>"true");
  72. }else{
  73. return false;
  74. }
  75. }else if($resultType=="autoid"){
  76. $data = M()->executeinsert($sqlString);
  77. if($data){
  78. return array("autoid"=>$data);
  79. }else{
  80. return "";
  81. }
  82. } else{
  83. $data = M()->execute($sqlString);
  84. if($data){
  85. return array("return"=>"true");
  86. }else{
  87. return false;
  88. }
  89. }
  90. }
  91. function delete($sqlString,$resultType){
  92. $data = M()->execute($sqlString);
  93. return $data;
  94. }
  95. /**
  96. * [array_to_sql 根据数组key和value拼接成需要的sql]
  97. * @param [type] $array [key, value结构数组]
  98. * @param string $type [sql类型insert,update]
  99. * @param array $exclude [排除的字段]
  100. * @return [string] [返回拼接好的sql]
  101. */
  102. function array_to_sql($array, $type='insert', $exclude = array()){
  103. $sql = '';
  104. if(count($array) > 0){
  105. foreach ($exclude as $exkey) {
  106. unset($array[$exkey]);//剔除不要的key
  107. }
  108. if('insert' == $type){
  109. $keys = array_keys($array);
  110. $values = array_values($array);
  111. $col = implode("`, `", $keys);
  112. $val = implode("', '", $values);
  113. $sql = "(`$col`) values('$val')";
  114. }else if('update' == $type){
  115. $tempsql = '';
  116. $temparr = array();
  117. foreach ($array as $key => $value) {
  118. $tempsql = "'$key' = '$value'";
  119. $temparr[] = $tempsql;
  120. }
  121. $sql = implode(",", $temparr);
  122. }
  123. }
  124. return $sql;
  125. }
  126. }
  127. ?>