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.

 

Link to comment
Share on other sites

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
Share on other sites

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.