2014-08-31 103 views
1

這是另一個MySql到MySqli的轉換我遇到了一些困難。將函數從MySql轉換爲MySqli

原始代碼:

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

新代碼:

define('DB_DEFAULT_FLAG', MYSQL_ASSOC); 

function resource_get($resource = NULL, $flag = DB_DEFAULT_FLAG) { 
    if (!$resource) 
     $resource = $this -> last_resource;//print_r($resource); 
    $return = mysqli_fetch_array($resource, $flag) or print("No workie!"); 
    var_dump($return); 
    return $return; 
} 

在這種情況下,函數調用失敗,出現「無Workie」響應,並在$返回空值變量。當我執行print_r($ resource)語句時,數據似乎是正確的。我究竟做錯了什麼?看起來mysqli_fetch_array語句失敗,但我不知道爲什麼。任何指針? mysqli_fetch_array或mysql_fetch_array顯然應該返回一個數組。

這裏是什麼的print_r($資源)返回一個例子:

Array ([0] => Array ([category] => American Swords [graphics] => 1 [user] => bquinn [i] => 1 [i_categories] => 133 [i_users] => 13 [i_users_modified] => 1 [date_created] => 1266697482 [date_modified] => 1398983308 [active] => 1 [sold] => 0 [filename] => bq203.htm [code] => BQ203 [name] => American Militia-style NCO Sword [cost] => 325.00 [price] => 425.00 [description] => Interesting fluted aluminum handle with gilded brass pommel and crossguard. Blade of lenticular cross-section with no nicks, age-discolored in spots. Showing no maker's markings. Leather scabbard in good condition with brass throat and acorn-finialed chape. Overall 37 5/8", blade 28". [weight] => 5 [height] => 6 [width] => 6 [length] => 48 [keywords] => American sword eagle head sword militia sword american saber 
+1

在打開'<?php'標記後立即向文件頂部添加錯誤報告 'error_reporting(E_ALL); ini_set('display_errors',1);'看看它是否產生任何東西。 – 2014-08-31 03:53:36

+0

是標誌'MYSQL_ASSOC'的nysql和nysqli是否一樣? – Ghost 2014-08-31 03:56:14

+0

確保資源在那裏,我敢打賭(因爲我們沒有看到錯誤日誌)您的連接不好。 – 2014-08-31 04:00:56

回答

1

在新的代碼,改變

define('DB_DEFAULT_FLAG', MySQL_ASSOC);

define('DB_DEFAULT_FLAG', MySQLi_ASSOC);

0

底層問題在於我的錯誤檢查。該函數在while循環中運行。通過在此函數內執行print_r()或var_dump(),循環的最後一次迭代就會拋出錯誤,因爲它已經用盡了數組行。該函數實際上工作正常,直到結束(在我修復了默認標誌問題之後)。感謝你的幫助!