alternative to mysql_field_name in mysqli

2019-02-09 23:14发布

问题:

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