Jump to content

mssql_fetch_field


4D

Recommended Posts

Hi.  Hope someone can help.

 

I have a PHP script that queries a mssql DB and puts the output into XML.

 

It conencts using mssql_connect, but I'm wanting to change it to use ODBC, as I want it be able to connect to SQL Server 2005 (I've not had much success getting the MS PHP driver working either).  This has been put together from info from the internet (I'm not a very good programmer you see) and so far it's gone well, however the part that does the XML transformation uses mssql_fetch_field, and there doesn't appear to be a odbc_fetch_field.

 

Is there an equivalent for mssql_fetch_field?

 

I'll put the full page below if that helps.  Many thanks.

 

<?php

//SQL Connection Info - update with your database, username & password

$connection = mssql_connect('server', 'username', 'password') or die ('cannot reach database');

$db = mssql_select_db("database") or die ("this is not a valid database");

 

//Change this query as you wish for single or multiple records

$result = mssql_query("SELECT trust_code, trust_name FROM tblTrust");

 

//Get the number of rows

$num_row = mssql_num_rows($result);

 

//Start the output of XML

echo '<?xml version="1.0" encoding="iso-8859-1"?>';

echo "<data>";

echo '<num>' .$num_row. '</num>';

if (!$result) {

  die('Query failed: ' . mssql_error());

}   

/* get column metadata - column name -------------------------------------------------*/

        $i = 0;

        while ($i < mssql_num_fields($result)) {

              $meta = mssql_fetch_field($result, $i);

            $ColumnNames[] = $meta->name;                      //place col name into array

            $i++;

        }

$specialchar = array("&",">","<");                                            //special characters

$specialcharReplace = array("&",">","<");            //replacement

/* query & convert table data and column names to xml ---------------------------*/

 

$w = 0;   

while ($line = mssql_fetch_array($result, MYSQL_ASSOC)) {

  echo "<row>";

    foreach ($line as $col_value){

        echo '<'.$ColumnNames[$w].'>';

        $col_value_strip = str_replace($specialchar, $specialcharReplace, $col_value);       

        echo $col_value_strip;

        echo '</'.$ColumnNames[$w].'>';

        if($w == ($i - 1)) { $w = 0; }

        else { $w++; }

      }

    echo "</row>";

}

echo "</data>";

mssql_free_result($result);

?>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/150693-mssql_fetch_field/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.