ANIL KUMAR PANIGRAHI 's Blog

How to display the mysql table schema using php code?

Posted by: Anil Kumar Panigrahi in: ● February 23, 2010

Hello friends,

The following script is for displaying the mysql connecting using php code and  table schema using php code.

It will be useful when we want to know the total fields of table.

1) Connecting with mysql :

$db_host     = “”;  // Database hostname
$db_username=”"; // Database username
$db_password=”"; // Database password
$db_name = “”; // Database name

$conn = mysql_connect($db_host,$db_username,$db_password) or die(“Could not connect to Server” .mysql_error());
mysql_select_db($db_name) or die(“Could not connect to Database” .mysql_error());

2) Write a query for the table which we want schema

$result = mysql_query(“SELECT * FROM {$tablename}”);
if (!$result) {
die(“Query to show fields from table failed”);
}

3) Getting the number of fields in that table

$fields_num = mysql_num_fields($result);

4) Display the table schema

for($i=0; $i<$fields_num; $i++)
{
$field = mysql_fetch_field($result);
echo “<b>{$field->name}</b>”;

echo “<br>”;

}

Share on Twitter

2 Comments to "How to display the mysql table schema using php code?"

1 | Gojeg

26 de February de 2010 to ● 1:06 am

I have not played with myql + PHP anymore .. But, thanks for reminding me.

2 | pell grant

13 de May de 2010 to ● 9:28 am

What a great resource!

Write Comment

About Me

  Anil Kumar Panigrahi
  Software Engineer (PHP)
  Blogger & Founder of Anil Labs
  Baruva, India

You can share with me . . .

My Own Contributions

1) First contribution for codeigniter framework ( multi language support using google translate API ). we can get files from http://github.com/nyros/codeigniter_multilanguage


2) Audio and Video Streaming using FFMpeg and PHP -complete doc file : http://www.docstoc.com/docs/8300349/Audio-and-Video-Streaming

Twitter Updates

Follow me @anil2u
  • http://www.google.co.uk/ logo is changing with mouse move . . . - posted on 07/09/2010 10:57:33
  • True enemies are real friends . . . - posted on 06/09/2010 10:15:29
  • Just watched nice movie " Madrasapattinam ". - posted on 05/09/2010 16:20:48
  • We will always be thankful to our teacher for all the hard work and efforts they have put in, for educating us.- Happy teachers Day . . . :) - posted on 05/09/2010 05:39:13
  • Working on shipping cost functionality (UPS and USPS) for oscommerce shopping cart - posted on 01/09/2010 09:40:03