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 that's 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);
YOU CAN USE MYSQLI ALTERNATIVE OF MYSQL EASY WAY php $con=mysqli_connect("localhost","my_user","my_password","my_db"); // Check connection if (mysqli_connect_errno()) { echo "Failed to connect to MySQL: " .
MySQL FIELD() FunctionThe FIELD() function returns the index position of a value in a list of values. This function performs a case-insensitive search. Note: If the specified value is not found in the list of values, this function will return 0. If value is NULL, this function will return 0.
Return Value: Returns an array of strings that corresponds to the fetched row. NULL if there are no more rows in result set. PHP Version: 5+
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With