Jump to content

[SOLVED] Get the name of a MYSQL field


otuatail

Recommended Posts

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.

 

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.

 

 

 

$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);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.