Forum Moderators: coopster

Message Too Old, No Replies

MySQL with PHP

how do I get a listing of the columns in a table

         

sanosuke001

1:41 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



Ok, I have made a MySQL table already,

mysql_query( "create table myTable ( field not null... etc )" )

and now I want to get a listing of those fields that I added because I want to be able to add and delete fields based on a different table. I tried doing

mysql_query( "describe myTable" )

but all I get is the first row in the table, not all the fields I added. I get in my array (after a mysql_fetch_array call) field, int(11), 0(which is what is stored in the first field) and then I don't get any of the other fields in the table. Anyone know a way for me to get a listing of the fields in a table? Thanks for the help.

sano

coopster

2:20 pm on Jul 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld, sanosuke001!

The fields will be in separate rows. Uncomment the line below to see the full array in each row:

$rows = mysql_query("DESCRIBE table"); 
while ($row = mysql_fetch_assoc($rows)) {
// print_r($row);
print $row['Field'] . '<br />';
}

Fixed typos

[edited by: coopster at 2:58 pm (utc) on July 8, 2004]

sanosuke001

2:44 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



Thanks, glad to be here ^_^

I don't know what you meant, but there is no mag_query or mag_fetch_assoc functions in PHP (that I know of...) and if there are, on runtime, it said that there was no such function. If you could revise what you wrote, that would be very appreciated.

sano

coopster

2:59 pm on Jul 8, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Yeah, I use a data abstraction layer. I just copied that chunk of code from a test script. The true mysql function names are there now.

sanosuke001

3:19 pm on Jul 8, 2004 (gmt 0)

10+ Year Member



Thank you very much. That is a great help.