otuatail Posted May 23, 2008 Share Posted May 23, 2008 I am trying to create a code page that is generic and will update a table. if I have a table called MyTable with for fields Name Address PostCode. Is there a way in PHP to get the names of the tables fields Desmond. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted May 23, 2008 Share Posted May 23, 2008 You can run the query SHOW COLUMNS FROM tablename and it will return a result set about the fields in that table Copy/paste Example #1 from http://us.php.net/manual/en/function.mysql-list-fields.php Quote Link to comment Share on other sites More sharing options...
otuatail Posted May 23, 2008 Author Share Posted May 23, 2008 Sort of but not what I wanted. I have this $SQL = "select * from Drinks"; $rsDraft = mysql_query($SQL); echo mysql_field_name($rsDraft, 0) . "<br>"; // first echo mysql_field_name($rsDraft, 6) . "<br>"; // Last echo mysql_num_rows($rsDraft); // result 9 rows. I need to know i have (7) fields Desmond. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 23, 2008 Share Posted May 23, 2008 $sql = "SHOW COLUMNS FROM Drinks"; $result = mysql_query($sql) or die(mysql_error()); $number_of_columns = mysql_num_rows($result); Quote Link to comment Share on other sites More sharing options...
otuatail Posted May 23, 2008 Author Share Posted May 23, 2008 No this is rows not colums $number_of_columns = mysql_num_rows($result); This will give the total number of records (rows) in the table. DROP TABLE IF EXISTS `Drinks`; CREATE TABLE IF NOT EXISTS `Drinks` ( `ID` int(11) NOT NULL auto_increment, `Category` tinyint(3) NOT NULL default '0', `Description` varchar(30) NOT NULL default '', `Pint` char(7) NOT NULL default '', `H_Pint` char(7) NOT NULL default '', `Happy`char(7) NOT NULL default '', `ABV` char(7) NOT NULL default '', PRIMARY KEY (`id`) ) TYPE=MyISAM; This table has 7 FIELDS. I need to know that not the total number of records in the entire table. mysql_num_rows() returns that. Quote Link to comment Share on other sites More sharing options...
BlueSkyIS Posted May 23, 2008 Share Posted May 23, 2008 $sql = "SHOW COLUMNS FROM Drinks"; // Returns a row for every column in the table ... so if you get the number of rows returned by that statement (mysql_num_rows()), you know how many columns are in the table Drinks. $sql = "SHOW COLUMNS FROM Drinks"; $result = mysql_query($sql) or die(mysql_error()); $number_of_columns = mysql_num_rows($result); Quote Link to comment Share on other sites More sharing options...
otuatail Posted May 23, 2008 Author Share Posted May 23, 2008 Ok guys that works. Been at this a long time. Didn't realise the query. Thanks a lot. will have hours of fun with this lot. Desmond. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.