If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > How to get column titles ?

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-19-04, 07:59
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
How to get column titles ?

I would like to ask whether there is a way to get all columns ids (titles) of any table using sql commands in php ?

Thanks
Reply With Quote
  #2 (permalink)  
Old 07-19-04, 08:08
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
Select column_name
from all_tab_columns
where table_name = 'tableA';
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #3 (permalink)  
Old 07-19-04, 08:19
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
Thanks for reply
But this is not what i need to do :

I put my question in another way :
The only thing I have is the table name, and I want to get all its columns names.
Reply With Quote
  #4 (permalink)  
Old 07-19-04, 08:22
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
Allow me to clarify.

Code:
SQL> create table a
  2  (
  3  col1 number(2),
  4  col2 number(2),
  5  col3 number(2)
  6  );

Table created.

SQL> select column_name
  2  from all_tab_columns
  3  where table_name = 'A';

COLUMN_NAME
------------------------------
COL1
COL2
COL3
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.

Last edited by r123456; 07-19-04 at 08:24.
Reply With Quote
  #5 (permalink)  
Old 07-19-04, 08:31
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
THanks again
But this command doesn't execute :
My base is in a library called "revue" so for a normal request I type :

$sql= 'select * from revue.My_table "
$tab=executeSQL($sql)

Where execute Sql is a function that return a table tht contains the sql result.

To test your solution I typed :
$sql= "select column_name
from all_tab_columns
where table_name = 'revue.user'";

And it doesnt works : The error sais that "ALL_TAB_COLUMNS doesnt exists in revue"

Thans for your patience
Reply With Quote
  #6 (permalink)  
Old 07-19-04, 08:35
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
Yes I see after you clarification
But considering this, my command I put above should work no ?
Reply With Quote
  #7 (permalink)  
Old 07-19-04, 08:37
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
The solution I provided was for an Oracle database.

SQL Server:

Select column_name
from information_schema.columns
where table_name = 'tableA'
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.

Last edited by r123456; 07-19-04 at 08:39.
Reply With Quote
  #8 (permalink)  
Old 07-19-04, 08:40
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
I'm using ibm database ( on a pc AS400 ) ... it must have clasic SQLs (87 and 91 (or 93 dont remember) )

Last edited by martones; 07-19-04 at 08:42.
Reply With Quote
  #9 (permalink)  
Old 07-19-04, 08:45
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
Try:

describe table revue.My_table
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #10 (permalink)  
Old 07-19-04, 08:51
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
error : The syntax element REVUE isn't correct
Reply With Quote
  #11 (permalink)  
Old 07-19-04, 08:55
r123456 r123456 is offline
Registered User
 
Join Date: Sep 2003
Location: The extremely Royal borough of Kensington, London
Posts: 778
__________________
Bessie Braddock: Winston, you are drunk!
Churchill: And Madam, you are ugly. And tomorrow, I'll be sober, and you will still be ugly.
Reply With Quote
  #12 (permalink)  
Old 07-19-04, 09:05
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
Thanks a lot again
But I still get errors :[
I tried : describe table revue.user
describe select * from revue.user

This gives errors

: The syntax element "." isn't correct

I execute this command with "odbc_do" php function
Reply With Quote
  #13 (permalink)  
Old 07-19-04, 10:04
MattR MattR is offline
Registered User
 
Join Date: Mar 2001
Location: Lexington, KY
Posts: 606
It's dirty but might work in a pinch:
$sql= 'select * from revue.My_table where 1=0'
$tab=executeSQL($sql)

That way it would give an empty result set with the column info. Obviously the best solution is to find a way to get the schema of a table without running a query, but it might be harder in PHP since there is no result set object or anything to give you column information (you'll have to parse it yourself).
__________________
Thanks,

Matt
Reply With Quote
  #14 (permalink)  
Old 07-19-04, 10:50
martones martones is offline
Registered User
 
Join Date: Jul 2004
Posts: 14
Yes this would work as I stand alone sql command but not with php for the reason you said

But thanks though Matt
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On