2013-09-23 20 views
6

Tak więc znalazłem tę wspaniałą funkcję, która konwertuje zapytania mysql na stronę XML i wygląda dokładnie tak, jak potrzebuję. Jedynym problemem jest to, że używa mysql, ale to nie jest już obsługiwane, i okazuje się, że jedna z używanych funkcji nie jest w mysqli. Czy ktoś wie o alternatywie do mysql_field_name?alternatywa dla mysql_field_name w mysqli

Oto funkcja, która znalazłem

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; 
} 

Z danej części jest

$fieldName = mysql_field_name($queryResult, $i); 

Dzięki

Mike

+0

można użyć [mysqli_fetch_fields()] (http://in2.php.net/manual/en/mysqli-result.fetch-fields.php) lub [mysqli_fetch_field()] (http://in2.php.net/manual/en/mysqli-result.fetch-field.php) – bansi

+0

co jeśli nie znam nazwy pola – user1685192

+0

'mysqli_fetch_fields()' zwraca tablicę pól – bansi

Odpowiedz