2014-08-31 117 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(),循环的最后一次迭代就会抛出错误,因为它已经用尽了数组行。该函数实际上工作正常,直到结束(在我修复了默认标志问题之后)。感谢你的帮助!