gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 12:44:57 25 4
gpt4 key购买 nike

我正在尝试更改以下代码以使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com