User Contributed Notes 12 notes

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:

$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)

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.

Sometimes the array keys of the results of odbc_columns() can be in lower case, instead of upper case (eg 'column_name' instead of 'COLUMN_NAME'). This can vary depending on which ODBC driver you are connecting to.

where:$connection is the result of your odbc_connect;$filename is the filename of the Excel file;$sheet is the name of the Excel worksheet.

This is useful when you want to query an Excel file without having to name ranges beforehand. With the results obtained from the above command, you can populate an array and use its contents (ie the column names) for further querying.

The result id returned from this function does not appear to behave exactly the same as a result id returned from a query.

I get all sorts of errors when I try to use subsequent odbc functions on the result id, such as:

odbc_fetch_into - This function only seems to work if I don't specify a row number or if the row number = 0.

odbc_fetch_row - This function will not return a row from the result set. Under some scenarios, I am able to use it in a while loop, but its seems buggy. Hence, my odbc_num_of_rows function (hack to replace the non-functional odbc_num_rows function) also doesn't work.

I recommend dumping the results into an array immediately and freeing the result id created by this function.

ps - If anyone has any further insight or workarounds, I would appreciate them.

Took me a while to figure out this command, thought I would save some other people the time. We couldn't quite figure out what the qualifier was. For MSSQL 7.0, it is the Database that you are connecing to. For the pubs database it would look something like this.