<?php
/*
@author waychar
@技术支持 150572314@qq.com
@copyright 2018 waychar.cn 声明：本代码可以用于学习交流使用及要商业化用途
*/
class db
{
    //db connect
    public function dbconnection()
    {
        $con = mysqli_connect(DB_IP, DB_USERNAME, DB_PASSWORD, DB_NAME);
        mysqli_query($con,"SET NAMES UTF8");
        if (!$con) {
            die('Could not connect:' . mysqli_error().'<br/>查看安装帮助:<a href=https://v.qq.com/x/page/o0757611u9c.html target=_blank>help.txt</a>');
        }
        return $con;
    }

    //get record
    public function get_data($sql, $db_name = DB_NAME)
    {
        if ($sql) {
            $result = mysqli_query($this->dbconnection(), $sql);
            if ($result) {
                while ($row = mysqli_fetch_array($result)) {
                    $out[] = $row;
                }
                return $out;
            }
        } else {
            return 'no date';
        }
    }

    //get record
    public function get_One($sql, $db_name = DB_NAME)
    {
        if ($sql) {
            mysqli_select_db($this->dbconnection(), DB_NAME);
            $result = mysqli_query($this->dbconnection(), $sql);
            if ($result) {
                while ($row = mysqli_fetch_array($result)) {
                    $out[] = $row;
                }
                return $out[0][0];
            }
        } else {
            return 'no date';
        }
    }

    public function query($sql, $db_name = DB_NAME)
    {
        if ($sql) {
            mysqli_select_db($this->dbconnection(), $db_name);
            $result = mysqli_query($this->dbconnection(), $sql);
        }
    }

    public function getmsg($table,$col,$id)
    {
        $sql ="select $col from $table where id=$id";
        $result = $this->get_One($sql);
        return $result;
    }
}