How to Retrieve a MySQL Table Structure in PHP

The "describe table" function lets you retrieve a list of columns and properties for a MySQL table from your PHP code. You can create a PHP website that lists table properties and columns using the MySQL connection libraries included with the PHP programming language. This means that you can retrieve columns before creating your MySQL queries that retrieve table values.

Instructions

    • 1

      Right-click the PHP file you want to edit and select "Open With." Click your preferred PHP editor in the submenu list of options.

    • 2

      Create the MySQL connection. The following code connects to the database server and selects the database:

      mysql_connect("server_name", "user", "pass");

      mysql_select_db("activedb") ;

      The first function connects to the server named "server_name" with the username

      "user" and password "pass." The database "activedb" is activated for the query. Replace these values with your own.

    • 3

      Set up the query for the table information. The following code creates a variable with the "describe table" query:

      $sql = "describe table";

      Replace "table" with the name of your database table.

    • 4

      Query the database and assign the results of the table to a variable. The following shows how to execute the "describe table" query:

      $tableinfo = mysql_fetch_array( $sql);

    • 5

      Display the first field. The results from the query return all of the table's columns. The following code displays the first column:

      echo $tableinfo[0];

Related Searches:

References

Comments

Related Ads

Featured