Newbie hopefully easy question

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
timbiotic
Forum Newbie
Posts: 14
Joined: Thu Feb 13, 2003 3:16 pm

Newbie hopefully easy question

Post 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:
fractalvibes
Forum Contributor
Posts: 335
Joined: Thu Sep 26, 2002 6:14 pm
Location: Waco, Texas

Post by fractalvibes »

What exactly do you mean by Headers?

Phil J.
timbiotic
Forum Newbie
Posts: 14
Joined: Thu Feb 13, 2003 3:16 pm

Post 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.
timbiotic
Forum Newbie
Posts: 14
Joined: Thu Feb 13, 2003 3:16 pm

Post 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
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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
timbiotic
Forum Newbie
Posts: 14
Joined: Thu Feb 13, 2003 3:16 pm

Thanks!

Post 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>";
}
?>
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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];

?>
timbiotic
Forum Newbie
Posts: 14
Joined: Thu Feb 13, 2003 3:16 pm

result died

Post 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
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post by Stoker »

what does the error say?
timbiotic
Forum Newbie
Posts: 14
Joined: Thu Feb 13, 2003 3:16 pm

Post 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
timbiotic
Forum Newbie
Posts: 14
Joined: Thu Feb 13, 2003 3:16 pm

Post 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?
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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
timbiotic
Forum Newbie
Posts: 14
Joined: Thu Feb 13, 2003 3:16 pm

Post 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?
User avatar
Stoker
Forum Regular
Posts: 782
Joined: Thu Jan 23, 2003 9:45 pm
Location: SWNY
Contact:

Post 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..
Post Reply