mysql.php

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· PHP 代码 · 共 1,557 行 · 第 1/4 页

PHP
1,557
字号
        } else {            if (($result = @mysql_query('SELECT VERSION()', $this->connection))) {                $version = explode('.', @mysql_result($result,0,0));                $major = intval($version[0]);                $minor = intval($version[1]);                $revision = intval($version[2]);                if ($major > 3 || ($major == 3 && $minor >= 23                    && ($minor > 23 || $revision >= 6)))                {                    $this->fixed_float = 0;                }                @mysql_free_result($result);            }        }        if (isset($this->supported['Transactions']) && !$this->auto_commit) {            if (!@mysql_query('SET AUTOCOMMIT = 0', $this->connection)) {                @mysql_close($this->connection);                $this->connection = 0;                $this->affected_rows = -1;                return($this->raiseError());            }            $this->in_transaction = TRUE;        }        $this->connected_host = $this->host;        $this->connected_user = $this->user;        $this->connected_password = $this->password;        $this->connected_port = $port;        $this->opened_persistent = $this->getoption('persistent');        return(MDB_OK);    }    // }}}    // {{{ _close()    /**     * all the RDBMS specific things needed close a DB connection     *     * @return boolean     * @access private     **/    function _close()    {        if ($this->connection != 0) {            if (isset($this->supported['Transactions']) && !$this->auto_commit) {                $result = $this->autoCommit(TRUE);            }            @mysql_close($this->connection);            $this->connection = 0;            $this->affected_rows = -1;            if (isset($result) && MDB::isError($result)) {                return($result);            }            unset($GLOBALS['_MDB_databases'][$this->database]);            return(TRUE);        }        return(FALSE);    }    // }}}    // {{{ query()    /**     * Send a query to the database and return any results     *     * @access public     *     * @param string  $query  the SQL query     * @param mixed   $types  array that contains the types of the columns in     *                        the result set     *     * @return mixed a result handle or MDB_OK on success, a MDB error on failure     */    function query($query, $types = NULL)    {        $this->debug("Query: $query");        $ismanip = MDB::isManip($query);        $this->last_query = $query;        $first = $this->first_selected_row;        $limit = $this->selected_row_limit;        $this->first_selected_row = $this->selected_row_limit = 0;        $result = $this->connect();        if (MDB::isError($result)) {            return($result);        }        if($limit > 0) {            if ($ismanip) {                $query .= " LIMIT $limit";            } else {                $query .= " LIMIT $first,$limit";            }        }        if ($this->database_name) {            if(!@mysql_select_db($this->database_name, $this->connection)) {                return($this->mysqlRaiseError());            }        }        if ($result = @mysql_query($query, $this->connection)) {            if ($ismanip) {                $this->affected_rows = @mysql_affected_rows($this->connection);                return(MDB_OK);            } else {                $result_value = intval($result);                $this->highest_fetched_row[$result_value] = -1;                if ($types != NULL) {                    if (!is_array($types)) {                        $types = array($types);                    }                    if (MDB::isError($err = $this->setResultTypes($result, $types))) {                        $this->freeResult($result);                        return($err);                    }                }                return($result);            }        }        return($this->mysqlRaiseError());    }    // }}}    // {{{ subSelect()    /**     * simple subselect emulation for Mysql     *     * @access public     *     * @param string $query the SQL query for the subselect that may only     *                      return a column     * @param string $quote determines if the data needs to be quoted before     *                      being returned     *     * @return string the query     */    function subSelect($query, $quote = FALSE)    {        if($this->supported['SubSelects'] == 1) {            return($query);        }        $col = $this->queryCol($query);        if (MDB::isError($col)) {            return($col);        }        if(!is_array($col) || count($col) == 0) {            return 'NULL';        }        if($quote) {            for($i = 0, $j = count($col); $i < $j; ++$i) {                $col[$i] = $this->getTextValue($col[$i]);            }        }        return(implode(', ', $col));    }    // }}}    // {{{ replace()    /**     * Execute a SQL REPLACE query. A REPLACE query is identical to a INSERT     * query, except that if there is already a row in the table with the same     * key field values, the REPLACE query just updates its values instead of     * inserting a new row.     *     * The REPLACE type of query does not make part of the SQL standards. Since     * practically only MySQL implements it natively, this type of query is     * emulated through this method for other DBMS using standard types of     * queries inside a transaction to assure the atomicity of the operation.     *     * @access public     *     * @param string $table name of the table on which the REPLACE query will     *  be executed.     * @param array $fields associative array that describes the fields and the     *  values that will be inserted or updated in the specified table. The     *  indexes of the array are the names of all the fields of the table. The     *  values of the array are also associative arrays that describe the     *  values and other properties of the table fields.     *     *  Here follows a list of field properties that need to be specified:     *     *    Value:     *          Value to be assigned to the specified field. This value may be     *          of specified in database independent type format as this     *          function can perform the necessary datatype conversions.     *     *    Default:     *          this property is required unless the Null property     *          is set to 1.     *     *    Type     *          Name of the type of the field. Currently, all types Metabase     *          are supported except for clob and blob.     *     *    Default: no type conversion     *     *    Null     *          Boolean property that indicates that the value for this field     *          should be set to NULL.     *     *          The default value for fields missing in INSERT queries may be     *          specified the definition of a table. Often, the default value     *          is already NULL, but since the REPLACE may be emulated using     *          an UPDATE query, make sure that all fields of the table are     *          listed in this function argument array.     *     *    Default: 0     *     *    Key     *          Boolean property that indicates that this field should be     *          handled as a primary key or at least as part of the compound     *          unique index of the table that will determine the row that will     *          updated if it exists or inserted a new row otherwise.     *     *          This function will fail if no key field is specified or if the     *          value of a key field is set to NULL because fields that are     *          part of unique index they may not be NULL.     *     *    Default: 0     *     * @return mixed MDB_OK on success, a MDB error on failure     */    function replace($table, $fields)    {        $count = count($fields);        for($keys = 0, $query = $values = '',reset($fields), $field = 0;            $field<$count;            next($fields), $field++)        {            $name = key($fields);            if ($field > 0) {                $query .= ',';                $values .= ',';            }            $query .= $name;            if (isset($fields[$name]['Null']) && $fields[$name]['Null']) {                $value = 'NULL';            } else {                if(isset($fields[$name]['Type'])) {                    switch ($fields[$name]['Type']) {                        case 'text':                            $value = $this->getTextValue($fields[$name]['Value']);                            break;                        case 'boolean':                            $value = $this->getBooleanValue($fields[$name]['Value']);                            break;                        case 'integer':                            $value = $this->getIntegerValue($fields[$name]['Value']);                            break;                        case 'decimal':                            $value = $this->getDecimalValue($fields[$name]['Value']);                            break;                        case 'float':                            $value = $this->getFloatValue($fields[$name]['Value']);                            break;                        case 'date':                            $value = $this->getDateValue($fields[$name]['Value']);                            break;                        case 'time':                            $value = $this->getTimeValue($fields[$name]['Value']);                            break;                        case 'timestamp':                            $value = $this->getTimestampValue($fields[$name]['Value']);                            break;                        default:                            return($this->raiseError(MDB_ERROR_CANNOT_REPLACE, NULL, NULL,                                'no supported type for field "' . $name . '" specified'));                    }                } else {                    $value = $fields[$name]['Value'];                }            }            $values .= $value;            if (isset($fields[$name]['Key']) && $fields[$name]['Key']) {                if ($value === 'NULL') {                    return($this->raiseError(MDB_ERROR_CANNOT_REPLACE, NULL, NULL,                        $name.': key values may not be NULL'));                }                $keys++;            }        }        if ($keys == 0) {            return($this->raiseError(MDB_ERROR_CANNOT_REPLACE, NULL, NULL,                'not specified which fields are keys'));        }        return($this->query("REPLACE INTO $table ($query) VALUES ($values)"));    }    // }}}    // {{{ getColumnNames()    /**     * Retrieve the names of columns returned by the DBMS in a query result.     *     * @param resource   $result    result identifier     * @return mixed                an associative array variable     *                              that will hold the names of columns. The     *                              indexes of the array are the column names     *                              mapped to lower case and the values are the     *                              respective numbers of the columns starting     *                              from 0. Some DBMS may not return any     *                              columns when the result set does not     *                              contain any rows.     *     *                              a MDB error on failure     * @access public     */    function getColumnNames($result)    {        $result_value = intval($result);        if (!isset($this->highest_fetched_row[$result_value])) {            return($this->raiseError(MDB_ERROR_INVALID, NULL, NULL,                'Get column names: it was specified an inexisting result set'));        }        if (!isset($this->columns[$result_value])) {            $this->columns[$result_value] = array();            $columns = @mysql_num_fields($result);            for($column = 0; $column < $columns; $column++) {                $field_name = @mysql_field_name($result, $column);                if ($this->options['optimize'] == 'portability') {                    $field_name = strtolower($field_name);                }                $this->columns[$result_value][$field_name] = $column;            }        }        return($this->columns[$result_value]);    }    // }}}    // {{{ numCols()    /**     * Count the number of columns returned by the DBMS in a query result.     *     * @param resource    $result        result identifier     * @access public     * @return mixed integer value with the number of columns, a MDB error     *                       on failure     */    function numCols($result)    {        $result_value = intval($result);        if (!isset($this->highest_fetched_row[$result_value])) {            return($this->raiseError(MDB_ERROR_INVALID, NULL, NULL,                'numCols: it was specified an inexisting result set'));        }        return(@mysql_num_fields($result));    }    // }}}    // {{{ endOfResult()    /**    * check if the end of the result set has been reached    *    * @param resource    $result result identifier    * @return mixed TRUE or FALSE on sucess, a MDB error on failure    * @access public    */    function endOfResult($result)    {        $result_value = intval($result);        if (!isset($this->highest_fetched_row[$result_value])) {            return($this->raiseError(MDB_ERROR, NULL, NULL,                'End of result: attempted to check the end of an unknown result'));        }        return($this->highest_fetched_row[$result_value] >= $this->numRows($result)-1);    }    // }}}    // {{{ fetch()    /**    * fetch value from a result set    *    * @param resource    $result result identifier    * @param int    $row    number of the row where the data can be found    * @param int    $field    field number where the data can be found    * @return mixed string on success, a MDB error on failure    * @access public    */    function fetch($result, $row, $field)    {        $result_value = intval($result);        $this->highest_fetched_row[$result_value] =            max($this->highest_fetched_row[$result_value], $row);        $res = @mysql_result($result, $row, $field);        if ($res === FALSE && $res != NULL) {            return($this->mysqlRaiseError());        }        return($res);

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?