<?php

//连接mysql类
class con
{
    public static $all = array(); //连接数组
    private $host = "127.0.0.1"; //数据库地址
    private $un = "root"; //用户名
    private $ps = "usestudio-1"; //密码
   private $db = "MaLai"; //密码
    // private $db = "shop"; //密码
    public $sql; //连接

//    private $host = "127.0.0.1"; //数据库地址
//    private $un = "root"; //用户名
//    private $ps = ""; //密码
//    private $db = "shop"; //密码
//    public $sql; //连接

    //处理结束
    public static function close($s)
    {
        if (is_resource($s)) {
            return mysqli_close($s);
        } else {
            return true;
        }
    }

    //构造连接数据库
    public function __construct()
    {
        //清除缓存
        ob_clean();
        $this->sql = new mysqli($this->host, $this->un, $this->ps, $this->db);
    }

    //mysql调用
    public static function usselect($p)
    {
        $a = explode(",", $p);
        $e = (new con());
        $e = $e->sql;
        $j = "";
        //错误处理
        if ($e->connect_error) {
            return "";
        }
        $e->set_charset("utf8");
        $s = "";

        $e->query("set character set 'utf8';");
        $res = "";
        $sqlname = urldecode(urldecode($a[0]));
        if (count($a) > 1) {
            for ($i = 1; $i < count($a); $i++) {
                $a[$i] = urldecode(urldecode($a[$i]));
            }
            $s = "CALL " . $sqlname . "('" . join("','", array_slice($a, 1)) . "');";
        } else {
            $s = "CALL " . $sqlname . "()";
        }
//       echo $s;
        $r = array();

        $e->multi_query(urldecode($s));
        do {
            $s = array();
            if ($res = $e->store_result()) {
                while ($v = $res->fetch_array(MYSQLI_ASSOC)) {
                    array_push($s, $v);
                }
                array_push($r, $s);
            }

        } while ($e->more_results() && $e->next_result());

        $j = json_encode(count($r) > 1 ? $r : (count($r) == 0 ? $e->affected_rows : $r[0]));
        con::close($e);
        return $j; //获取返回的值
    }

    //回收占用问题
    function __destruct()
    {
        con::close($this->sql);
    }
}

?>
