How to display mysql table fields using PHP
It is common to find the mysql table field names in the given table while creating dynamic models for our web applications.To continue this tutorial we have to create a test table. Below mysql query is used to create this test table.
CREATE TABLE `testtable` ( `test_id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `test_name` VARCHAR(45) NOT NULL, `test_created` DATETIME NOT NULL, PRIMARY KEY (`test_id`) ) ENGINE = InnoDB;
SHOW COLUMNS FROM TABLENAME query is used for list the mysql fields.
below code lists the mysql table fields.
<?php //replace it with your host normally it could be localhost $hostname='localhost'; //mysql user name $username='username'; //mysql user password $password='password'; //connect to the mysql server $ss = mysql_pconnect($hostname, $username, $password) or trigger_error(mysql_error(),E_USER_ERROR); //select a database on the mysql server //please change as you like the database name mysql_select_db('mydatabase'); //SHOW COLUMNS FROM TABLENAME $query=mysql_query('SHOW COLUMNS FROM testtable ') or die(mysql_error()); //iterate trough the query result and fetch each field while($field=mysql_fetch_object($query)){ $fields[]=$field;//collect each field into a array }; print_r($fields);//test the fields array foreach($fields as $key=>$field){ echo $field->Field.'</br>'; // print each field name } ?>
Popularity: 7% [?]









Related Articles
2 users responded in this post
[...] more: How to display mysql table fields using PHP | Php Development If you enjoyed this article please consider sharing [...]
[...] rest is here: How to display mysql table fields using PHP | Php Development Share and [...]
Leave A Reply