PHP-Mysql 操作类问题

WBOY
Lepaskan: 2016-06-06 20:45:16
asal
1081 orang telah melayarinya

简单的操作类

<code><?php /**
*+-------------------------------
*| @todo mysql 操作类
*+-------------------------------
**/

    class MYSQL{

        protected $conn;
        protected $stmt;
        public function __construct($serverName,$userName,$password,$database,$port='3306',$charset="utf8")
        {
            $server = $serverName.':'.$port;
            $this->conn = mysql_connect($server,$userName,$password,true) or die('connect MySQL DB error!');
            mysql_select_db($database,$this->conn) or die('select db error!');
            mysql_query("set names ".$charset,$this->conn);
        }

        /**
        *+-------------------------------
        *|  @param $type  select,insert,delete,updata
        *+-------------------------------
        *
        **/

        public function query($sqlQuery,$type)
        {

            $this->stmt = mysql_query($sqlQuery);

            if($this->stmt)
            {
                switch ($type) {
                    case 'select':

                        $data = array();
                        while ($row = mysql_fetch_array($this->stmt,MYSQL_ASSOC)) {
                            $data[] = $row;
                        }

                        return $data;

                        break;

                    case 'insert':
                        return mysql_insert_id();
                        break;
                    case 'update':
                        return mysql_affected_rows();
                        break;
                    case 'delete':
                        return mysql_affected_rows();
                        break;
                    default:
                        return "Please,enter '\$type'";
                        break;  
                }
            }

        }

        function __destruct(){

            @mysql_free_result($this->stmt);
            mysql_close($this->conn);
        }
    }
</code>
Salin selepas log masuk
Salin selepas log masuk

调用方法

<code><?php require_once('mysql.class.php');
    $njhl = new MYSQL("192.168.1.222","root","test","nj100hl");
    $hshdb = new MYSQL("192.168.1.222","root","test","hsh_database");
    if(time() >= mktime(12,0,0))
    {
        $begin = mktime(0,0,0);
        $end   = mktime(11,59,59);
    }
    else
    {
        $begin = mktime(12,0,0,date("m"),date("d")-1,date("Y"));
        $end = mktime(23,59,59,date("m"),date("d")-1,date("Y"));
    }


    $begin = mktime(0,0,0,1,1,2013);
    $end   = mktime(0,0,0,12,31,2014);

    $orderQuery = "select sum(goods.goods_nums) as `goods_nums`,goods.goods_id as `goods_id` 
                    from iwebshop_order_goods as goods left join iwebshop_order as `order` on `order`.id = goods.order_id 
                    where `status` = '5' and unix_timestamp(`order`.completion_time) 
                    between '".$begin."' and '".$end ."' group by goods.goods_id";

    $orderResult =  $njhl->query($orderQuery,'select');

    var_dump($orderResult);
</code>
Salin selepas log masuk
Salin selepas log masuk

首先

<code>select sum(goods.goods_nums) as `goods_nums`,goods.goods_id as `goods_id` 
                    from iwebshop_order_goods as goods left join iwebshop_order as `order` on `order`.id = goods.order_id 
                    where `status` = '5' and unix_timestamp(`order`.completion_time) 
                    between '1356969600' and '1419955200' group by goods.goods_id
</code>
Salin selepas log masuk
Salin selepas log masuk

这个语句是可以查询到值得。

不知道为什么var_dump($orderResult) 确是NULL, 可能是实例化同一个类有问题!
不知道这个类该怎么写?

回复内容:

简单的操作类

<code><?php /**
*+-------------------------------
*| @todo mysql 操作类
*+-------------------------------
**/

    class MYSQL{

        protected $conn;
        protected $stmt;
        public function __construct($serverName,$userName,$password,$database,$port='3306',$charset="utf8")
        {
            $server = $serverName.':'.$port;
            $this->conn = mysql_connect($server,$userName,$password,true) or die('connect MySQL DB error!');
            mysql_select_db($database,$this->conn) or die('select db error!');
            mysql_query("set names ".$charset,$this->conn);
        }

        /**
        *+-------------------------------
        *|  @param $type  select,insert,delete,updata
        *+-------------------------------
        *
        **/

        public function query($sqlQuery,$type)
        {

            $this->stmt = mysql_query($sqlQuery);

            if($this->stmt)
            {
                switch ($type) {
                    case 'select':

                        $data = array();
                        while ($row = mysql_fetch_array($this->stmt,MYSQL_ASSOC)) {
                            $data[] = $row;
                        }

                        return $data;

                        break;

                    case 'insert':
                        return mysql_insert_id();
                        break;
                    case 'update':
                        return mysql_affected_rows();
                        break;
                    case 'delete':
                        return mysql_affected_rows();
                        break;
                    default:
                        return "Please,enter '\$type'";
                        break;  
                }
            }

        }

        function __destruct(){

            @mysql_free_result($this->stmt);
            mysql_close($this->conn);
        }
    }
</code>
Salin selepas log masuk
Salin selepas log masuk

调用方法

<code><?php require_once('mysql.class.php');
    $njhl = new MYSQL("192.168.1.222","root","test","nj100hl");
    $hshdb = new MYSQL("192.168.1.222","root","test","hsh_database");
    if(time() >= mktime(12,0,0))
    {
        $begin = mktime(0,0,0);
        $end   = mktime(11,59,59);
    }
    else
    {
        $begin = mktime(12,0,0,date("m"),date("d")-1,date("Y"));
        $end = mktime(23,59,59,date("m"),date("d")-1,date("Y"));
    }


    $begin = mktime(0,0,0,1,1,2013);
    $end   = mktime(0,0,0,12,31,2014);

    $orderQuery = "select sum(goods.goods_nums) as `goods_nums`,goods.goods_id as `goods_id` 
                    from iwebshop_order_goods as goods left join iwebshop_order as `order` on `order`.id = goods.order_id 
                    where `status` = '5' and unix_timestamp(`order`.completion_time) 
                    between '".$begin."' and '".$end ."' group by goods.goods_id";

    $orderResult =  $njhl->query($orderQuery,'select');

    var_dump($orderResult);
</code>
Salin selepas log masuk
Salin selepas log masuk

首先

<code>select sum(goods.goods_nums) as `goods_nums`,goods.goods_id as `goods_id` 
                    from iwebshop_order_goods as goods left join iwebshop_order as `order` on `order`.id = goods.order_id 
                    where `status` = '5' and unix_timestamp(`order`.completion_time) 
                    between '1356969600' and '1419955200' group by goods.goods_id
</code>
Salin selepas log masuk
Salin selepas log masuk

这个语句是可以查询到值得。

不知道为什么var_dump($orderResult) 确是NULL, 可能是实例化同一个类有问题!
不知道这个类该怎么写?

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan
Tentang kita Penafian Sitemap
Laman web PHP Cina:Latihan PHP dalam talian kebajikan awam,Bantu pelajar PHP berkembang dengan cepat!