当mysqlnd驱动程序不可用时,是否有fetch_field_direct()的替代方法?

时间:2013-11-24 03:23:04

标签: php mysqli

我很满意我在这里得到的回应:How to fetch field type and value?。它工作正常,我可以像建议的那样轻松获取数据类型:

$datatype = $result->fetch_field_direct($count)->type;

其中$count是一个使用foreach循环递增的数字

然后我开始尝试PDO到prevent SQL injections,一切都很好(在路上经过几次颠簸,因为我不完全理解PDO,我承认),直到我决定上传我的代码到网络服务器,然后我发现他们不支持使用mysqld,这意味着我不能再使用get_result函数

$result = $stmt->get_result();

虽然我找到了get_result的解决方法(posted in the php reference comment section),但我还没有找到fetch_field_direct()的替代品,我知道如何获取数据类型?也许解决方法代码可能会有所帮助(它超出了我对php的理解水平,但在模拟get_result()的基本功能方面做得非常好。我可以这样做:

$result = iimysqli_stmt_get_result($stmt); 
$proceso = iimysqli_result_fetch_array($result);
...

foreach(array_keys($proceso) as $key){

$datatype = $result->fetch_field_direct($count)->type; // this is no longer working, 
//I get error Fatal error: Call to undefined method iimysqli_result::fetch_field_direct()
// It used to work when $result was equal to $stmt->get_result(); 

... // example line
 echo "<input class='$mysql_data_type_hash[$datatype]' type='hidden' name='$key' value='$proceso[$key]'><br/>";
...

}

我想知道是否可以使用iimysqli_result_fetch_field_direct()方法来克服无法使用标准方法。如果是这样,怎么样? (至少从哪里开始)

为了让它更清楚:我希望你的帮助知道是否有办法扩展我在php参考页面中找到的变通方法,这样我就可以得到一个字段的数据类型。除非另有说明,否则我不得不放弃它,并努力解决我无法使用$stmt->get_result()功能的事实。

我已经阅读了一些关于使用$ stmt-&gt; bind_result()的内容,但是当一个人有很多字段要处理时似乎很难用,所以我想避免它(如果可能的话) ),因为解决方法(下面的代码)已经帮助我获取值(我只需要弄清楚数据类型)。


get_result()的解决方法(不是我的,我没有写它,说实话我不完全理解它,但它有效),这是评论的直接副本:

<?php
class iimysqli_result
{
    public $stmt, $nCols;
}    

function iimysqli_stmt_get_result($stmt)
{
    /**    EXPLANATION:
     * We are creating a fake "result" structure to enable us to have
     * source-level equivalent syntax to a query executed via
     * mysqli_query().
     *
     *    $stmt = mysqli_prepare($conn, "");
     *    mysqli_bind_param($stmt, "types", ...);
     *
     *    $param1 = 0;
     *    $param2 = 'foo';
     *    $param3 = 'bar';
     *    mysqli_execute($stmt);
     *    $result _mysqli_stmt_get_result($stmt);
     *        [ $arr = _mysqli_result_fetch_array($result);
     *            || $assoc = _mysqli_result_fetch_assoc($result); ]
     *    mysqli_stmt_close($stmt);
     *    mysqli_close($conn);
     *
     * At the source level, there is no difference between this and mysqlnd.
     **/
    $metadata = mysqli_stmt_result_metadata($stmt);
    $ret = new iimysqli_result;
    if (!$ret) return NULL;

    $ret->nCols = mysqli_num_fields($metadata);
    $ret->stmt = $stmt;

    mysqli_free_result($metadata);
    return $ret;
}

function iimysqli_result_fetch_array(&$result)
{
    $ret = array();
    $code = "return mysqli_stmt_bind_result(\$result->stmt ";

    for ($i=0; $i<$result->nCols; $i++)
    {
        $ret[$i] = NULL;
        $code .= ", \$ret['" .$i ."']";
    };

    $code .= ");";
    if (!eval($code)) { return NULL; };

    // This should advance the "$stmt" cursor.
    if (!mysqli_stmt_fetch($result->stmt)) { return NULL; };

    // Return the array we built.
    return $ret;
}
?>

1 个答案:

答案 0 :(得分:1)

毕竟这很简单(我想我只需休息一下就能看到它)

在php帮助评论中找到的解决方法缺少我需要的两个功能

function iimysqli_result_fetch_assoc(&$result)

function iimysqli_result_fetch_field_direct(&$result, $i)

以下是我对它们的实现:

 function iimysqli_result_fetch_field_direct(&$result, $i)
 {
    $metadata = mysqli_stmt_result_metadata($result->stmt); 
    return mysqli_fetch_field_direct($metadata,$i);   
 }

 function iimysqli_result_fetch_assoc(&$result)
 {
   $ret = array();
   $code = "return mysqli_stmt_bind_result(\$result->stmt ";   
   for ($i=0; $i<$result->nCols; $i++)
   {
       $finfo = iimysqli_result_fetch_field_direct($result,$i) ;
       $name =  $finfo->name;

       $ret[$name] = NULL;
       $code .= ", \$ret['" .$name ."']";
   };
   $code .= ");";
   if (!eval($code)) { return NULL; };

   // This should advance the "$stmt" cursor.
   if (!mysqli_stmt_fetch($result->stmt)) { return NULL; };

   // Return the array we built.
   return $ret;
    }

我不确定这是最佳解决方案,但它对我来说到目前为止工作:)