Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alternative to mysql_field_name in mysqli

Tags:

php

mysql

mysqli

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);
like image 809
mikemmb73 Avatar asked Sep 23 '13 03:09

mikemmb73


People also ask

How can I get column from database in PHP?

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: " .

What is field name in 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.

What is $row in PHP?

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+


1 Answers

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

like image 71
trakos Avatar answered Sep 28 '22 11:09

trakos