-1

Following snippet outputs the field names present in a mysql query:

for($i=0; $i<$fields_num; $i++)
{

    $field = mysql_fetch_field($result);
    echo  $field->name , ",";  
    //outputs something like ID,FirstName,OrderNbr etc..
}   

Obviously this only works with a mysql result handle ($result).

How do you do the same thing when the statement handle is sqlsrv?

EDIT: We all pretty much know that mysql out, and mysqli in. But the question is not about that.

Average Joe
  • 4,103
  • 8
  • 45
  • 75
  • [`mssql_fetch_field`](http://www.php.net/manual/en/function.mssql-fetch-field.php)? – bfavaretto Aug 07 '12 at 19:51
  • Using the mssql extension or PDO? – CodeZombie Aug 07 '12 at 19:54
  • 3
    You should stop using `mysql_*` functions. They're being deprecated. Instead use [PDO](http://php.net/manual/en/book.pdo.php) (supported as of PHP 5.1) or [mysqli](http://php.net/manual/en/book.mysqli.php) (supported as of PHP 4.1). If you're not sure which one to use, [read this SO article](http://stackoverflow.com/questions/13569/mysqli-or-pdo-what-are-the-pros-and-cons). – Matt Aug 07 '12 at 19:55
  • @Matt: These comment should be automatically added whenever someone uses the mysql extensions :-) – CodeZombie Aug 07 '12 at 19:56
  • @ZombieHunter i'm surprised there's no meta question on this – Matt Aug 07 '12 at 19:57
  • I get a Call to undefined function mssql_fetch_field(). All my mssql work is done with sqlsrv_ way not the mssql_ way. I checked for the existence of `sqlsrv_fetch_field()` but there isn't. – Average Joe Aug 07 '12 at 19:59
  • @ZombieHunter I just added this as a feature request to meta. – Matt Aug 07 '12 at 20:00
  • @ZombitHunter [this was the response I received, almost immediately](http://stackapps.com/questions/2116/autoreviewcomments-pro-forma-comments-for-se). – Matt Aug 07 '12 at 20:03
  • @Matt: Well, this still needs to be added manually. I'd prefer a bot :-). But thanks for investigating – CodeZombie Aug 07 '12 at 20:17
  • Hey Matt, what's a meta question anyway? – Average Joe Aug 07 '12 at 21:03

3 Answers3

2

Thanks to the ZombieHunter's lead, here is the answer to my question.

Included here for those who might need it.

    foreach( sqlsrv_field_metadata($result) as $fieldMetadata)
    {
        echo $fieldMetadata['Name'] , ",";  
        //outputs something like ID,FirstName,OrderNbr etc..
    }           
Average Joe
  • 4,103
  • 8
  • 45
  • 75
1

It depends on the database extension you're using:

nickb
  • 56,839
  • 11
  • 91
  • 130
  • thank you nickb, but I get a `Call to undefined function mssql_fetch_field()` error. I forgot to mention in my question that all the microsoft sql work I deal with is done thru the sqlsrv_ way not the mssql_ way. I checked for the existence of sqlsrv_fetch_field() but there isn't. The solution must be based on sqlsrv_ methods or it won't work for me. I cannot get into the PDO now neither cause I'm working with a non PDO library - unfortunately. – Average Joe Aug 07 '12 at 20:07
1

Try sqlsrv_field_metadata().

As far as I can see there is nothing else available. Check the API Reference on MSDN for a complete list of all supported functions.

CodeZombie
  • 5,097
  • 3
  • 26
  • 37