alternative to mysql_field_name in mysqli

那年仲夏 提交于 2020-01-10 19:27:25

问题


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?

Here's the function that I found

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; 
}

With the part in question is

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

Thanks

Mike


回答1:


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/en/mysqli-result.fetch-field-direct.php



来源:https://stackoverflow.com/questions/18951467/alternative-to-mysql-field-name-in-mysqli

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!