我想将我的 mysql 查询转换为XML,为此我使用了本教程的代码:http: //www.codediesel.com/php/converting-mysql-queries-to-xml/我必须将其自定义为使用PDO代替mysql函数。这是代码:
function sqlToXml($queryResult, $rootElementName, $childElementName)
{
$xmlData = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\" ?>\n";
$xmlData .= "<" . $rootElementName . ">";
while($record = $queryResult->fetch(PDO::FETCH_OBJ))
{
/* Create the first child element */
$xmlData .= "<" . $childElementName . ">";
for ($i = 0; $i < $queryResult->columnCount(); $i++)
{
$fieldName = $queryResult->getColumnMeta($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;
}
但是当我尝试执行代码时出现此错误:注意:第42行/opt/lampp/htdocs/promos/t.php中的数组到字符串转换
注意:第42行/opt/lampp/htdocs/promos/t.php中的数组到字符串转换
注意:第42行/opt/lampp/htdocs/promos/t.php中的数组到字符串转换
注意: /opt/lampp/htdocs/promos/t.php中第42行的数组到字符串的转换nullnullnullnull
第42行是:
if(!empty($record->$fieldName))
你对此有什么想法吗?谢谢 :)