php - 在 MySql 到 MySqli 转换后处理 MySQLi 查询的结果

标签 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 的值最终只是“Array()”。因此,据我所知,这部分代码没有正确处理某些内容,这就是为什么我包含了 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) 只产生“Array()”。

最佳答案

当您将变量$resource传递给$this->resource_get()时,它并不代表mysqli_result资源对象。相反,它已经是一个二维结果数组,因为您之前运行了 mysqli_fetch_assoc() 循环。

要使其适用于您当前的代码,您可以删除之前的获取循环:

$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 code> 即资源对象。

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

关于php - 在 MySql 到 MySqli 转换后处理 MySQLi 查询的结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25584902/

相关文章:

php - Inpage urdu 到 unicode 乌尔都语转换器

php - MySQL INSERT 查询从另一个表中获取值,包括一个变量

无法释放 C 中的内存 - 动态结构和数组

C : How to make the size of an array dynamic?

php - 将临时结果存储在单独的数据库表中

php - Laravel Redis 队列忽略限制

php - 以尽可能最佳的规范化方式将可变数量的数据输入数据库

javascript 内的 php - 未找到内容

php - 如何修复通知 : Object of class PDOStatement could not be converted to int

sql - PostgreSQL:根据数组(外键)列连接 2 个表