How do I convert a mssql_field_name function to sqlsrv

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pavithra_infog
Forum Newbie
Posts: 2
Joined: Thu Nov 10, 2016 11:27 pm

How do I convert a mssql_field_name function to sqlsrv

Post by pavithra_infog »

Hi,

I am converting the mssql functions in my application to sqlsrv functions as part of a migration activity from SQL Server 2003 to SQL Server 2008. I am not getting the expected results when I just replace mssql_field_name with its equivalent in sqlsrv i.e sqlsrv_field_metadata. I am not sure if I am using the right sqlsrv function for this. Could anyone help me here. The code which I am trying to convert looks something like this:

Code: Select all

<?php
$result = mssql_query($query, $conn);
$rows  = mssql_num_rows($result);
$cols = mssql_num_fields($result);

if(rows>0)
{
for($i= 0;$i<$cols;++$i)
{
$colname = mssql_field_name($result,$i);
echo " <th>$colname</th>";
}
echo "</tr>";
}

if($rows<=0){
echo "No Results found for the specified inputs";
exit();
}else
{
    for($i = 0;$i < $rows; $i++)
   {
echo "<tr>";
for($j =0;$j < $cols; $j++)
{
$colname = mssql_field_name($result,$j);
$out = mssql_result($result,$i,"$colname");
echo "<td> $out &nbsp;</td>";
}
echo "</tr>";
    }

echo "<br>";
}
?>
Last edited by Celauran on Fri Nov 11, 2016 8:09 am, edited 1 time in total.
Reason: Please wrap your code in syntax tags
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: How do I convert a mssql_field_name function to sqlsrv

Post by Christopher »

sqlsrv_field_metadata($stmt) returns an array of arrays. There is an array of information for each field. I think you can do something similar to what you are doing like this:

Code: Select all

foreach (sqlsrv_field_metadata($stmt) as $colname => $metadata) {
    echo " <th>$colname or {$metadata['Name']}</th>";
}
See: https://msdn.microsoft.com/en-us/library/cc296197.aspx
(#10850)
pavithra_infog
Forum Newbie
Posts: 2
Joined: Thu Nov 10, 2016 11:27 pm

Re: How do I convert a mssql_field_name function to sqlsrv

Post by pavithra_infog »

Hi Christopher,

Thanks for providing the syntax of equivalent sqlsrv function for mssql_field_name. I am now able to get the expected results after changing the code snippet.


Best Regards,
Pavithra
Post Reply