Page 1 of 1

Newbie hopefully easy question

Posted: Thu Feb 13, 2003 3:16 pm
by timbiotic
I am trying to pull Headers from an Access table to propogate a dropdown box. I currently connect with ODBC connection. How can I get the table header names? Do I have to switch to ADO? Or is there a simple SQL statement that can pull headers as a table property?

Thanks
timbiotic :oops:

Posted: Thu Feb 13, 2003 10:17 pm
by fractalvibes
What exactly do you mean by Headers?

Phil J.

Posted: Fri Feb 14, 2003 10:05 am
by timbiotic
Basically i need to pull the field names... The field titles... The things at the top of the table that describe the fields. I need them so when someone chooses them in the drop down box, my SQL statement will use the fields selected. But i need to pull them from the table because they will be changed frequently and I dont want to hardcode them.

Posted: Mon Feb 17, 2003 11:51 am
by timbiotic
I was told to use dbx_query () by a friend. Does anyone know how I could use this to pull the field descriptions? The table name is tblPers

thanks,
timbiotic

Posted: Mon Feb 17, 2003 12:32 pm
by Stoker
if you have done a rowfetching query you get the column names as the non-numeric keys of the associative array returned by odbc_fetch_array(), or if you are fetching one column at the time there is odbc_field_name ();

If you have not done a query you can use odbc_columns ()

Doing it with a query I don't know Bill Gates SQL very well, for mysql it would be doable with DESCRIBE table

Thanks!

Posted: Mon Feb 17, 2003 1:42 pm
by timbiotic
Using the odbc_field_name I was able to pull what I wanted. But being able to pull into an array directly would be even better. How would I word the odbc_fetch_array command to pull the Field Names right into the array? Again thanks, and if I have misunderstood the fetch array function then nevermind. I have what i need!

Here is what I did:

<?php
$conn = odbc_connect("2003","***","***")
or die("Could not connect");

$query = "SELECT * FROM tblPers";
$result = odbc_exec($conn, $query) or die('Select failed!');
$i = 0;
$fCount = odbc_num_fields($result);

while ($i < $fCount)
{
$i++;
$fName = odbc_field_name($result, $i);
$aFNarray[$i] = $fName ;
echo "$aFNarray[$i]<BR>";
}
?>

Posted: Mon Feb 17, 2003 2:02 pm
by Stoker
Doing SELECT * if you don't need any of the data is a big waste of resources, if you only want the column names:

Code: Select all

<?php

$conn = odbc_connect("2003","***","***")
or die("Could not connect");

$result =  odbc_columns($conn, '%', '', 'tblPers' ) or die(odbc_error_msg());

$column_name = array();
while ( $column = odbc_fetch_array($result) ) {
  $column_name[] = $column['COLUMN_NAME'];
}

echo 'The thirc column is named '.$column_name[2];

?>

result died

Posted: Mon Feb 17, 2003 2:17 pm
by timbiotic
I tried that but for some reason I can not get odbc_columns to work. I always get the die result when it is executed

Posted: Mon Feb 17, 2003 2:28 pm
by Stoker
what does the error say?

Posted: Mon Feb 17, 2003 2:34 pm
by timbiotic
Fatal error: Call to undefined function: odbc_fetch_array() in C:\Inetpub\wwwroot\stage.dominionsystems.com\web\field\test.php3 on line 8

I am on 4.1.1 in case you wondered

Posted: Mon Feb 17, 2003 2:35 pm
by timbiotic
Also, when I put this line in:
$result = odbc_columns($conn, '%', '', 'tblPers' ) or die("Failed");

I get the Failed message returned.... I noticed you had an errortrapper, do you have a link to that function?

Posted: Mon Feb 17, 2003 2:39 pm
by Stoker
odbc_fetch_array was added in PHP 4.0.2, what version are you running? (use phpinfo(); to find out)

It is not an errortrapper, it is an odbc function that will output the ODBC's last error message, I actually spelled it wrong, it is odbc_errormsg(), has been in existence since PHP 4.0.5

Posted: Mon Feb 17, 2003 3:33 pm
by timbiotic
I am on 4.1.1 in case you wondered
From above....

Might the extension be disabled? And if do, how do i enable it?

Posted: Mon Feb 17, 2003 6:22 pm
by Stoker
if odbc_connect () works it should be enabled (?) Check phpinfo() for odbc info, perhaps there are some differences with the bill gates version of php? I don't know really..