我想将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;
}但是,当我试图执行代码时,我得到了以下错误:注意:在/opt/lampp/htdocs/promos/t.php中,42中的数组到字符串的转换
注意:在/opt/lampp/htdocs/promos/t.php中,42中的数组到字符串转换
注意:在/opt/lampp/htdocs/promos/t.php中,42中的数组到字符串转换
注意:/opt/lampp/htdocs/promos/t.php中的数组到字符串转换(42)中的 nullnullnullnull
42行是:
if(!empty($record->$fieldName))你知道这件事吗?谢谢您:)
https://stackoverflow.com/questions/13347196
复制相似问题