我的PHP查询功能不能使用INSERT查询

时间:2015-04-08 02:08:43

标签: php mysql database mysqli sql-insert

我在下面创建了MySQLi查询函数,它可以很好地处理SELECT查询并动态执行所有操作,因此它可以处理任何大小的结果集,但是当我尝试INSERT INTO时,我得到了message 致命错误:在布尔上调用成员函数fetch_field()。我希望它根据查询是否成功,为TRUEFALSE等查询返回INSERT INTOUPDATE。有什么建议吗?

这是完整的查询功能:

    /**
     * Querying the database
     *
     * @param string $query_string The query string to be executed
     * @param array $params The parameters for the query if it is a prepared statement
     *
     * @return mixed The results of the query, either in an array or NULL, or FALSE if executing the query failed
     */
    final public function query($query_string, $params = array()) {

        // Resetting error and result data
        $this->error = FALSE;
        $this->result = array();
        $this->result_metadata = array();

        // Initiating the query statement
        $this->stmt = $this->DB->stmt_init();

        // Preparing the query statement and binding parameters if provided
        if ($this->stmt->prepare($query_string) and count($params)) {

            // Setting the types of each parameter in $params
            $param_types = "";
            foreach ($params as $param) {
                if (is_int($param)) {
                    $param_types .= "i"; continue;
                }
                if (is_float($param)) {
                    $param_types .= "d"; continue;
                }
                if (is_string($param)) {
                    $param_types .= "s"; continue;
                }
                else {
                    $param_types .= "b";
                }
            }

            // Appending the parameter types to the beginning of $params for use in call_user_func_array
            array_unshift($params, $param_types);

            // Creating a reference array and passing all values from $params into it by reference
            $reference = array();
            foreach ($params as $key => $param) {
                $reference[$key] = &$params[$key];
            }

            // Binding the parameters to the query statement
            call_user_func_array(array($this->stmt, "bind_param"), $reference);
        }

        // Executing the query statement
        if (@$this->stmt->execute()) {

            // Fetching the metadata of the result
            $this->result_metadata = $this->stmt->result_metadata();

            // Populating $fields with variables whose name is the value of $field_name indexed under $field_name
            while ($field = $this->result_metadata->fetch_field()) {
                $field_name = $field->name;
                $$field_name = NULL;
                $fields[$field_name] = &$$field_name;
            }

            // Binding the results of the query to $fields
            call_user_func_array(array($this->stmt, "bind_result"), $fields);

            // Storing the results of the query
            $this->stmt->store_result();

            // Fetching the results of the query and storing them in $result_array
            $row = 0;
            while ($this->stmt->fetch()) {
                $result_array[$row] = array();
                foreach ($fields as $key => $field) {
                    $result_array[$row][$key] = $field;
                }
                $row++;
            }

            // Storing the number of rows to self::$num_rows
            $this->num_rows = $this->stmt->num_rows;                

            // Closing the query statement
            $this->stmt->free_result();
            $this->stmt->close();

            // Returning $result_array, or the only element of $result_array if $result_array is of size 1
            return @((count($result_array) === 1) ? $result_array[0] : $result_array);
        }

        // Setting $this->error equal to the DB error and returning FALSE
        else {
            $this->error = $this->DB->error;
            return FALSE;
        }

    }

1 个答案:

答案 0 :(得分:1)

根据the docsresult_metadata函数只会在查询返回结果集时返回一个对象。由于INSERT查询仅报告成功/失败,因此result_metadata仅返回false。有关详细信息,请参阅this answer

但是因为它只返回FALSE,所以当然不能在其上调用fetch_field