php mysqli 中 mysql_field_name 的替代方案
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18951467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
alternative to mysql_field_name in mysqli
提问by mikemmb73
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?
所以我发现了这个将mysql查询转换为XML页面的很棒的函数,它看起来正是我需要的。唯一的问题是它使用了 mysql,但不再受支持,而且结果使用的函数之一不在 mysqli 中。有谁知道 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
麦克风
回答by trakos
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
http://www.php.net/manual/en/mysqli-result.fetch-field-direct.php

