mssql_field_name

(PHP 4, PHP 5, PECL odbtp >= 1.1.1)

mssql_field_nameLit le nom d'un champ MS SQL Server

Description

string mssql_field_name ( resource $result [, int $offset = -1 ] )

Retourne le nom du champ numéro offset du résultat result.

Liste de paramètres

result

La ressource de résultats à évaluer. Ce résultat provient d'un appel à la fonction mssql_query().

offset

La position du champ, en commençant à 0. Si ce paramètre est omis, le champ courant sera utilisé.

Valeurs de retour

Le nom du champ spécifié en cas de succès ou FALSE si une erreur survient.

Exemples

Exemple #1 Exemple avec mssql_field_name()

<?php
// Envoi une requête de type SELECT à MSSQL
$query mssql_query('SELECT [username], [name], [email] FROM [php].[dbo].[userlist]');

echo 
'Le jeu de résultat contient le(s) champ(s) suivant(s) :'PHP_EOL;

// Affiche le nom de tous les champs du jeu de résultats
for ($i 0$i mssql_num_fields($query); ++$i) {
    echo 
' - ' mssql_field_name($query$i), PHP_EOL;
}

// Libère le jeu de résultats
mssql_free_result($query);
?>

L'exemple ci-dessus va afficher quelque chose de similaire à :

Le jeu de résultat contient le(s) champ(s) suivant(s) :
 - username
 - name
 - email

Voir aussi

add a note add a note

User Contributed Notes 3 notes

up
0
ian_channing at hotmail dot com
4 months ago
There is a limitation of 30 characters for the field name. So if your query has  `fieldname AS really_long_overly_descriptive_name`, you will only get `really_long_overly_descriptiv` back

<?php
// Send a select query to MSSQL
$query = mssql_query('SELECT [username] AS [really_long_overly_descriptive_name], [name], [email] FROM [php].[dbo].[userlist]');

echo
'Result set contains the following field(s):', PHP_EOL;

// Dump all field names in result
for ($i = 0; $i < mssql_num_fields($query); ++$i) {
    echo
' - ' . mssql_field_name($query, $i), PHP_EOL;
}

// Free the query result
mssql_free_result($query);
?>

Outputs the following:

Result set contains the following field(s):
 - really_long_overly_descriptiv
 - name
 - email
up
0
harold at worby dot dns2go dot com
12 years ago
A simple script to walk through an MSSQL server.
I'm using PHP 4.06 and Freetds 5.1 on Solaris 2.7 and Apache 1.3
Several of the msql_ functions seem to be broken/missing.
This is what I found to work.

<?php
// --------------------------------------------------------
// URL http://server/mssqltest.php?DB=Database&table=TableName
// --------------------------------------------------------
// Default settings: edit to meet your needs
if (!($user)) $user=sa;
if (!(
$pass)) $pass="password";
if (!(
$host)) $host="acer900";
// --------------------------------------------------
// connect to mssql server
$connect = mssql_connect($host,$user,$pass) or die ($host." not accessible.");
// select the database
if ($DB) mssql_select_db($DB)or die('USE '.$DB.' failed!');
if (!(
$table)) {
   
$query="EXEC sp_tables \"%\",\"%\",\"".$DB."\",\"'TABLE'\"";
   
$linkcol=2;
   
$linkcol="TABLE_NAME";
$linkformat=" <a href=\"%s?DB=".$DB."&table=%s\">%s</a> ";
}else{
// tables and DB - Get DATA
   
$query="SELECT * FROM ".$table;
   
$linkformat=" <a href=\"%s\">%s</a> ";
}
if (!(
$DB))    {
   
$query="EXEC sp_databases";
   
$linkcol=0;
   
$linkcol="DATABASE_NAME";
   
$linkformat="<a href=\"%s?DB=%s\">%s</a>";
}

// run query
echo $query."<br>";
$result = mssql_query( $query) or die('Query failed!');
$fields = mssql_num_fields ($result) or die("Num Fields Failed");
$rows   = mssql_num_rows ($result);

echo
"<br>Rows ".$rows." Fields ".$fields."<br>";
echo
"<TABLE border=1><tr>";
// get field names
for ( $f = 0 ; $f < $fields ; $f++ ){
   
$name = mssql_fetch_field($result, $f);
    echo
"<td>".$f.$name->name."</td>";
// Strange here.. without the "X"'s the () is always TRUE??
   
if ($name->name."X" == $linkcol."X") $linkcol = $f;
}
echo
"</tr>";
//Display Data
for ($i = 0; $i < $rows ; $i =$i +1){
    for (
$f = 0; $f<$fields ; $f++){
       
$name=mssql_result($result,$i,$f);
        if (
$f == $linkcol){
printf("<td>".$linkformat."</td>\n",$PHP_SELF,
 
$name , $name );
        }
        else{
            echo
"<td>". $name . "</td>";
        }
    }
echo
"</tr>";
}
echo
"</TABLE><br>";

// close connection
mssql_close ($connect);
?>
up
0
tengel at sonic dot net
13 years ago
This function isn't supported (PHP 4.0.2) if using Sybase/FreeTDS to access SQLServer (linux/solaris/bsd/etc).  Instead, use "mssql_fetch_field()" like so:

$field_name = mssql_fetch_field($query_result, $field_number);
print $field_name->name;
To Top