php - 替代 mysqli 中的 mysql_field_name

标签 php mysql mysqli

我发现了这个很棒的函数,可以将 MySQL 查询转换为 XML 页面,它看起来正是我需要的。唯一的问题是它使用 MySQL,但不再支持它,结果发现其中一个使用的函数不在 MySQLi 中。有谁知道 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);

最佳答案

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

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

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

关于php - 替代 mysqli 中的 mysql_field_name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18951467/

相关文章:

mysql - 使用 glassfish 的数据库连接的持久性异常

php - 从 php 的 mysql 查询中转义字符

PHP 在多个查询和表上插入和更新

javascript - 为表中的每一行创建mysql删除链接,使用ajax删除它并更新表列表

javascript - 在灯箱中加载 Paypal 上下文结帐,几秒钟后恢复为经典快速结帐

javascript - 保护 AJAX 调用的 PHP 端点

javascript - 将调用的 JavaScript 文件中的函数应用到 PHP echo

php - 在特定时间向所有用户发布信息的 Facebook 应用程序 (php)

mysql - 从 Mysql 获取行并使用 Django 将其显示在 html 中

php - 错误号 : 1048 Column 'btc' cannot be null