-1

为什么我仍然得到一个

警告:mysql_fetch_assoc() 期望参数 1 是资源,字符串在

使用以下功能时:?

public function getRowsWhere($table, $order_by, $where_field, $where_match, $limit_start = 0, $limit_end = 1) {

    // query Table
    $query_string = "SELECT * FROM $table WHERE $where_field='$where_match' ORDER BY '$order_by' DESC LIMIT $limit_start, $limit_end";

    // run query
    $sql    = new DBQuery($this->database);
    $result = $sql->prepareQuery($query_string);

    if ($result) {
       // initialize results array
        $match_array = [];

        // iterate through array and place results in an array at row index
        $r = 0;
        while ($row = mysql_fetch_assoc($result)) {
            $row_array = [];
            $c       = 0;
            while ($c < mysql_num_fields($result)) {
                $col                   = mysql_fetch_field($result, $c);
                $row_array[$col->name] = $row[$col->name];
                $c++;
            }
            $match_array[$r] = $row_array;
            $r++;
        }
    }

    // return data
    return $match_array;
}

从以下调用:

if (!$breadcrumbs->getCrumb(2)) {
    $project_details = $copy->getRows('projects', 'id', 'DESC', 0, 1);
}
else {
    $project_details = $copy->getRowsWhere('projects', 'id', 'id', $breadcrumbs->getCrumb(2), 0, 1);
    if (!$project_details) {
        $project_details = $copy->getRows('projects', 'id', 'DESC', 0, 1);
    }
}

根据要求:DBQuery 类。这都是相当基本的东西。

<?php

class DbQuery extends DbMan {

    /**
     *   PARAMETERS
     *
     * ^ @param array  $database            -> database connection information
     * --------------------------------------------------------------------------
     */
    protected $database;

    /**
     * --------------------------------------------------------------------------
     *   PUBLIC
     *   __construct ()
     * = set database object to use
     *
     * ^ @param string  $database           -> database connection information
     * --------------------------------------------------------------------------
     *
     */
    public function __construct($database) { $this->database = $database; }

    /**
     * --------------------------------------------------------------------------
     *   PUBLIC
     *   prepareQuery ()
     * = checks Sql, executes query to return requested data set
     *
     * ^ @param  string  $query_request     -> query string
     * ~ @return object  $result_dataset    -> result data set
     * --------------------------------------------------------------------------
     *
     */
    public function prepareQuery($query_request) {

        // retrieve result dataset
        $result_dataset = $this->executeQuery($query_request);

        // return result data set
        return $result_dataset;
    }
}

它扩展的类 DBMan:

<?php
class DbMan {

    /**
     *   PARAMETERS
     *
     * ^ @param array   $database           -> database connection information
     *   @param object  $mysql_connection   -> database connection object
     *   @param object  $mysql_connection   -> database object
     * --------------------------------------------------------------------------
     */
    protected $database;
    private   $mysql_connection;
    private   $mysql_db;

    /**
     * --------------------------------------------------------------------------
     *   PUBLIC
     *   __construct ()
     * = empty constructor
     *
     * ^ @param        ->
     * --------------------------------------------------------------------------
     *
     */
    public function __construct() { }

    /**
     * --------------------------------------------------------------------------
     *   PROTECTED
     *   dbConnect ()
     * = open connection to MySql and connect to database
     *
     * ^ @param        ->
     * --------------------------------------------------------------------------
     *
     */
    protected function dbConnect() {

        // connect to MySQL
        $this->mysql_connection = mysql_connect($this->database['hostname'], $this->database['username'], $this->database['password'])
            or die('Unable to connect to MySQL : ' . mysql_error());

        // connect to database
        $this->mysql_db = mysql_select_db($this->database['database'], $this->mysql_connection)
            or die('Unable to connect to database : ' . mysql_error());
    }

    /**
     * --------------------------------------------------------------------------
     *   PROTECTED
     *   executeQuery ()
     * = run query on database
     *
     * ^ @param  string  $query          -> sql statement string
     * ~ @return result  $query_result   -> result of query
     * --------------------------------------------------------------------------
     *
     */
    protected function executeQuery($query) {

        // open connection to the database
        $this->dbConnect();

        // run sql query on database
        $query_result = mysql_query($query)
            or die('Could not run query on table:<br />' . mysql_error());

        // close database connection
        mysql_close($this->mysql_connection);

        // Check to make sure data is returned
        if (mysql_num_rows($query_result)) {
            // return result data set
            return $query_result;
        }
        else {
            // return nothing
            return "No Data Available";
        }
    }
}

我希望这能让这个更清楚..

4

1 回答 1

0

你在这里关闭连接:

    // close database connection
    mysql_close($this->mysql_connection);

    // Check to make sure data is returned
    if (mysql_num_rows($query_result)) {
        // return result data set
        return $query_result;
    }
    else {
        // return nothing
        return "No Data Available";
    }

这意味着mysql_num_rows返回FALSE,因此返回字符串No Data Available,因此返回mysql_fetch_assoc() expects parameter 1 to be resource, string given错误。

于 2013-03-31T11:17:59.293 回答