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. Link to comment https://forums.phpfreaks.com/topic/106984-solved-get-the-name-of-a-mysql-field/ 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 Link to comment https://forums.phpfreaks.com/topic/106984-solved-get-the-name-of-a-mysql-field/#findComment-548371 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. Link to comment https://forums.phpfreaks.com/topic/106984-solved-get-the-name-of-a-mysql-field/#findComment-548404 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); Link to comment https://forums.phpfreaks.com/topic/106984-solved-get-the-name-of-a-mysql-field/#findComment-548411 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. Link to comment https://forums.phpfreaks.com/topic/106984-solved-get-the-name-of-a-mysql-field/#findComment-548424 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); Link to comment https://forums.phpfreaks.com/topic/106984-solved-get-the-name-of-a-mysql-field/#findComment-548429 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. Link to comment https://forums.phpfreaks.com/topic/106984-solved-get-the-name-of-a-mysql-field/#findComment-548440 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.