我試圖更改以下代碼以使用MySqli而不是MySql。我刪除了一些對我在這裏要解決的問題並不重要的方法。處理MySql之後的MySQLi查詢結果到MySqli轉換
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(結果)和$資源數組正確加載,但使用print_r()檢查時,$ return的值最終會變成「Array()」。因此,儘可能接近我自己看着辦,自己是不是被正確的這部分代碼,這就是爲什麼我包括resource_get()函數調用處理:
$return = array();
while($data = $this->resource_get($resource, $flag))
$return[] = $data;
//print_r($return);
break;
如果我使用mysqli_fetch_row($資源, $ flag)而不是mysqli_fetch_array($ resource,$ flag)我仍然得到相同的結果,即print_r($ return)只產生「Array()」。
看起來像'$ resource' _already_包含你的結果數組,因爲while($ row = mysqli_fetch_assoc($ result)){$ resource [] = $ row; }'。當你將'$ resource'傳遞給'$ this-> resource_get()'時,它對'mysqli_fetch_array()'的調用得到一個數組而不是結果資源。如果您已經獲取了行,請不要打擾'resource_get()'方法。要自己驗證一下,只需在'switch'或'switch:「show」'之前'print_r($ resource)'。 – 2014-08-30 18:38:07
這表明你沒有'display_errors'打開,因爲'resource_get()'應該引發警告。總是在開發代碼時,'error_reporting(E_ALL); ini_set('display_errors',1);' – 2014-08-30 18:39:10
謝謝,邁克爾。這解決了它。我剛剛刪除了while循環,並使$ return = $資源,它的工作原理!這不是我的代碼,我坦率地不明白爲什麼我需要調用resource_get()和MySqli,我想我不知道。 – user1780242 2014-08-30 21:12:24