note 91544 added to function.odbc-columns

From: Date: Tue, 16 Jun 2009 13:46:14 +0000
Subject: note 91544 added to function.odbc-columns
Groups: php.notes 
Request: Send a blank email to php-notes+get-156918@lists.php.net to get a copy of this message
[MS SQL Server 2005/2008, PHP 5] Imagine you would need to access the column names of a specific table, for instance to display them as table headers for fields with missing information. While browsing the documentation I was kind of lost how to use odbc_columns() without the usage of odbc_result_all() which outputs EVERYTHING in a single HTML table. Here is a way to stuff all output into an array and then access only one or more fields of the odbc_columns() output: <?php include('connect.inc'); // <== Put all your database connection parameters in here. (DSN, PWD, USR, mssql_connect, etc.; returns $connection) $outval = odbc_columns($connection, "your DB name", "%", "your table name", "%"); $pages = array(); while (odbc_fetch_into($outval, $pages)) { echo $pages[3] . "<br />\n"; // presents all fields of the array $pages in a new line until the array pointer reaches the end of array data } ?> Now your array $pages will have the following contents: ([x] is the array index displayed here for better understanding) [0] TABLE_CAT <== your DB name [1] TABLE_SCHEM <== dbo, your table scheme [2] TABLE_NAME <== your table name [3] COLUMN_NAME <== your column names (selected all with "%" in odbc_columns() ) [4] DATA_TYPE <== -8 [5] TYPE_NAME <== nchar (corresponds to -8, 11 f.i. is datetime and so on) [6] COLUMN_SIZE <== num. val. [7] BUFFER_LENGTH <== num. val. [8] DECIMAL_DIGITS <== num. val. or NULL [9] NUM_PREC_RADIX <== num. val. or NULL [10] NULLABLE <== num. val. [11] REMARKS <== num. val. or NULL [12] COLUMN_DEF <== num. val. or NULL [13] SQL_DATA_TYPE <== num. val. [14] SQL_DATETIME_SUB <== num. val. or NULL [15] CHAR_OCTET_LENGTH <== num. val. or NULL [16] ORDINAL_POSITION <== num. val. [17] IS_NULLABLE <== YES/NO [18] SS_DATA_TYPE <== num. val. Now you can access each field recursivly by its key and output only the DESIRED fields instead of having ALL output from odbc_result_all(). Please note that the array key starts at zero (0) instead of one (1), so echo $pages[3] selects COLUMN_NAME from the above list. I hope this helps... Cheers Thomas ---- Server IP: 217.160.72.57 Probable Submitter: 217.67.37.209 ---- Manual Page -- http://www.php.net/manual/en/function.odbc-columns.php Edit -- https://master.php.net/note/edit/91544 Del: integrated -- https://master.php.net/note/delete/91544/integrated Del: useless -- https://master.php.net/note/delete/91544/useless Del: bad code -- https://master.php.net/note/delete/91544/bad+code Del: spam -- https://master.php.net/note/delete/91544/spam Del: non-english -- https://master.php.net/note/delete/91544/non-english Del: in docs -- https://master.php.net/note/delete/91544/in+docs Del: other reasons-- https://master.php.net/note/delete/91544 Reject -- https://master.php.net/note/reject/91544 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#156918) next »