db_Mysql::query PHP Method

query() public method

执行查询 返回数据集
public query ( string $str ) : mixed
$str string sql指令
return mixed
    public function query($str)
    {
        $this->initConnect(false);
        if (!$this->_linkID) {
            return false;
        }
        $this->queryStr = $str;
        //释放前次的查询结果
        if ($this->queryID) {
            $this->free();
        }
        //N('db_query',1);
        // 记录开始执行时间
        //G('queryStartTime');
        $this->queryID = mysql_query($str, $this->_linkID);
        $this->debug();
        if (false === $this->queryID) {
            $this->error();
            return false;
        } else {
            if (0 === stripos($str, 'call')) {
                // 存储过程查询支持
                $this->close();
                $this->linkID = array();
            }
            $this->numRows = mysql_num_rows($this->queryID);
            return $this->getAll();
        }
    }

Usage Example

Example #1
0
function db_query_mysql($sql, &$res, $timeout = 0, $type = '0', $key = null)
{
    #echo "MC:".($type)."#<br>";
    global $dbhost, $dbuser, $dbpassword, $dbname;
    $db = new db_Mysql();
    $db->init_con($dbhost, $dbuser, $dbpassword, $dbname);
    $db->create_con();
    $result = $db->query($sql);
    if ($result == false) {
        $db->free();
        $db->close();
        return false;
    } else {
        $res = array();
        while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
            $res[] = $row;
        }
        global $memcached_host;
        $options = array('servers' => array($memcached_host), 'debug' => false, 'compress_threshold' => 510240, 'persistant' => false);
        $mc = new memcached($options);
        if ($type == "1") {
            //add type
            $mc->add($key, $res, $timeout);
        }
        if ($type == "2") {
            //replace
            $mc->replace($key, $res, $timeout);
        }
        $mc->disconnect_all();
    }
    $db->free();
    $db->close();
    return true;
}