在MySql到MySqli转换后处理来自MySQLi查询的结果

时间:2014-08-30 17:11:20

标签: php mysql arrays mysqli

我正在尝试更改以下代码以使用MySqli而不是MySql。我已经删除了一些似乎不重要的方法,我在这里解决的问题。

class db {
    var $hostname,
        $database,
        $username,
        $password,
        $connection,
        $last_query,
        $last_i,
        $last_resource,
        $last_error;

    function db($hostname=DB_HOSTNAME,$database=DB_DATABASE,$username=DB_USERNAME,$password=DB_PASSWORD) {
        $this->hostname = $hostname;
        $this->database = $database;
        $this->username = $username;
        $this->password = $password;

        $this->connection = mysql_connect($this->hostname,$this->username,$this->password) or $this->choke("Can't connect to database");
        if($this->database) $this->database($this->database);
        }

    function database($database) {
        $this->database = $database;
        mysql_select_db($this->database,$this->connection);
        }

    function query($query,$flag = DB_DEFAULT_FLAG) {
        $this->last_query = $query;

        $resource = mysql_query($query,$this->connection) or $this->choke();
        list($command,$other) = preg_split("|\s+|", $query, 2);

        // Load return according to query type...
        switch(strtolower($command)) {
            case("select"):
            case("describe"):
            case("desc"):
            case("show"):
                $return = array();
                while($data = $this->resource_get($resource,$flag)) $return[] = $data;
                //print_r($return);
                break;
            case("replace"):
            case("insert"):
                if($return = mysql_insert_id($this->connection))
                    $this->last_i = $return;
                break;
            default:
                $return = mysql_affected_rows($this->connection);
            }

        return $return;
        }

    function resource_get($resource = NULL,$flag = DB_DEFAULT_FLAG) {
        if(!$resource) $resource = $this->last_resource;
        return mysql_fetch_array($resource,$flag);
        }   
    }

这是我到目前为止所得到的:

class db {
    var $hostname = DB_HOSTNAME,
        $database = DB_DATABASE,
        $username = DB_USERNAME,
        $password = DB_PASSWORD,
        $connection,
        $last_query,
        $last_i,
        $last_resource,
        $last_error;

    function db($hostname, $database, $username, $password) {
        $this->hostname = $hostname;
        $this->database = $database;
        $this->username = $username;
        $this->password = $password;

        $this->connection = new mysqli($this->hostname, $this->username, $this->password, $this->database) or $this->choke("Can't connect to database");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


        if($this->database)
            $this->database($this->database);
    }

    function database($database) {
        $this->database = $database;
        mysqli_select_db($this->connection, $this->database );
    }

    function query($query, $flag = DB_DEFAULT_FLAG) {
        $this->last_query = $query;
//print_r($query);
        $result = mysqli_query($this->connection, $query) or $this->choke("problem connecting to DB");
while($row=mysqli_fetch_assoc($result)) {
$resource[]=$row;
}
//print($command);
//print_r($resource);print("<br>");
        list($command, $other) = preg_split("|\s+|", $query, 2);

        // Load return according to query type...
        switch(strtolower($command)) {
            case("select"):
            case("describe"):
            case("desc"):
            case("show"):
                $return = array();
                while($data = $this->resource_get($resource, $flag))
                    $return[] = $data;
                //print_r($return);
                break;
            case("replace"):
            case("insert"):
                if($return = mysqli_insert_id($this->connection))
                    $this->last_i = $return;
                break;
            default:
                $return = mysqli_affected_rows($this->connection);
            }

        return $return;
        }

    function resource_get($resource = NULL, $flag = DB_DEFAULT_FLAG) {
        if(!$resource)
            $resource = $this->last_resource;
        return mysqli_fetch_array($resource, $flag);
    }

所以问题就在于:我用print_r()检查了结果,并且$ resource数组正确加载,但是当用print_r()检查时,$ return的值最终变成了&#34;阵列()&#34 ;.因此,尽可能接近我在这部分代码中正确处理的事情,这就是为什么我包含了resource_get()函数调用:

            $return = array();
            while($data = $this->resource_get($resource, $flag))
                $return[] = $data;
            //print_r($return);
            break;

如果我使用mysqli_fetch_row($ resource,$ flag)而不是mysqli_fetch_array($ resource,$ flag),我仍然得到相同的结果,即print_r($ return)简单地产生&#34; Array()&#34;

1 个答案:

答案 0 :(得分:1)

变量$resource在您将mysqli_result传递给$this->resource_get()时不代表mysqli_fetch_assoc()资源对象。相反,它已经是一个2D结果数组,因为您之前运行了$result = mysqli_query($this->connection, $query) or $this->choke("problem connecting to DB"); // Remove this //while($row=mysqli_fetch_assoc($result)) { // $resource[]=$row; //} 循环。

要使用当前代码,您可以删除先前的提取循环:

$result

稍后,将$resource代替resource_get()传递到$result方法,因为它是resource_get()资源对象。

或者,您可以完全跳过$resource调用并直接返回{{1}},因为它已包含结果数组。