在mysqli中替代mysql_field_name [英] alternative to mysql_field_name in mysqli

查看:447
本文介绍了在mysqli中替代mysql_field_name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我发现了一个很棒的函数,它将mysql查询转换为XML页面,看起来确实正是我所需要的.唯一的问题是它使用mysql,但现在不再支持,结果发现使用的功能之一不在mysqli中.有人知道mysql_field_name的替代方法吗?

So I found this great function that converts mysql queries into a XML page, and it looks like exactly what I need. The only problem is that it uses mysql, but thats not supported anymore, and it turns out one of the functions used isn't in mysqli. Does anyone know of an alternative to mysql_field_name?

这是我发现的功能

function sqlToXml($queryResult, $rootElementName, $childElementName)
{ 
$xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n"; 
$xmlData .= "<" . $rootElementName . ">";

while($record = mysql_fetch_object($queryResult))
{ 
    /* Create the first child element */
    $xmlData .= "<" . $childElementName . ">";

    for ($i = 0; $i < mysql_num_fields($queryResult); $i++)
    { 
        $fieldName = mysql_field_name($queryResult, $i); 

        /* The child will take the name of the table column */
        $xmlData .= "<" . $fieldName . ">";

        /* We set empty columns with NULL, or you could set 
            it to '0' or a blank. */
        if(!empty($record->$fieldName))
            $xmlData .= $record->$fieldName; 
        else
            $xmlData .= "null"; 

        $xmlData .= "</" . $fieldName . ">"; 
    } 
    $xmlData .= "</" . $childElementName . ">"; 
} 
$xmlData .= "</" . $rootElementName . ">"; 

return $xmlData; 
}

有问题的部分是

$fieldName = mysql_field_name($queryResult, $i);

谢谢

迈克

推荐答案

有很多方法可以做到,我想最相似的是:

There are many ways to do it, I guess the most similar would be:

$fieldName = mysqli_fetch_field_direct($result, $i)->name;

http://www.php.net/manual/zh-CN/mysqli-result.fetch-field-direct.php

这篇关于在mysqli中替代mysql_field_name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆