mysql.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. //连接mysql类
  3. class con
  4. {
  5. public static $all = array(); //连接数组
  6. private $host = "127.0.0.1"; //数据库地址
  7. private $un = "root"; //用户名
  8. private $ps = "usestudio-1"; //密码
  9. private $db = "MaLai"; //密码
  10. // private $db = "shop"; //密码
  11. public $sql; //连接
  12. // private $host = "127.0.0.1"; //数据库地址
  13. // private $un = "root"; //用户名
  14. // private $ps = ""; //密码
  15. // private $db = "shop"; //密码
  16. // public $sql; //连接
  17. //处理结束
  18. public static function close($s)
  19. {
  20. if (is_resource($s)) {
  21. return mysqli_close($s);
  22. } else {
  23. return true;
  24. }
  25. }
  26. //构造连接数据库
  27. public function __construct()
  28. {
  29. //清除缓存
  30. ob_clean();
  31. $this->sql = new mysqli($this->host, $this->un, $this->ps, $this->db);
  32. }
  33. //mysql调用
  34. public static function usselect($p)
  35. {
  36. $a = explode(",", $p);
  37. $e = (new con());
  38. $e = $e->sql;
  39. $j = "";
  40. //错误处理
  41. if ($e->connect_error) {
  42. return "";
  43. }
  44. $e->set_charset("utf8");
  45. $s = "";
  46. $e->query("set character set 'utf8';");
  47. $res = "";
  48. $sqlname = urldecode(urldecode($a[0]));
  49. if (count($a) > 1) {
  50. for ($i = 1; $i < count($a); $i++) {
  51. $a[$i] = urldecode(urldecode($a[$i]));
  52. }
  53. $s = "CALL " . $sqlname . "('" . join("','", array_slice($a, 1)) . "');";
  54. } else {
  55. $s = "CALL " . $sqlname . "()";
  56. }
  57. // echo $s;
  58. $r = array();
  59. $e->multi_query(urldecode($s));
  60. do {
  61. $s = array();
  62. if ($res = $e->store_result()) {
  63. while ($v = $res->fetch_array(MYSQLI_ASSOC)) {
  64. array_push($s, $v);
  65. }
  66. array_push($r, $s);
  67. }
  68. } while ($e->more_results() && $e->next_result());
  69. $j = json_encode(count($r) > 1 ? $r : (count($r) == 0 ? $e->affected_rows : $r[0]));
  70. con::close($e);
  71. return $j; //获取返回的值
  72. }
  73. //回收占用问题
  74. function __destruct()
  75. {
  76. con::close($this->sql);
  77. }
  78. }
  79. ?>