PHP code to display on your page any table from MySQL db

By Sergey Skudaev

+++++

By Sergey Skudaev

Here you will find PHP code example that pulls data from MySQL database and displays it on the form. If you are developing a PHP application you have to display records from different tables and each time you have to write HTML code for table with different column numbers and names. This PHP code example is universal for any query. As a result you can use the same php template that reads meta data: counts fields, reads field names and creates a suitable HTML table. Pulls fields values and display data inside the table. You can create any table in MySQL database, write any SQL query and copy it in place of my query in my code example.

Contents:
1. How to install Pocket PC emulator on your Windows PC
2. Three columns layout CSS styles for regular PC browsers
3. One column layout CSS styles for mobile device

css styles for mobile device

Pay via PayPal. You should not be concern about my web site security, because when you click the link, you will be redirected to PayPal site and it can be trusted. You will have your financial transaction with PayPal, not with me. So, please do not afraid to buy from my site. Credit cards are accepted. After payment is done, on the PayPal thank you page

click the Return to Merchant link.
You will be redirected to the download page! Also, You will receive email with link to download the tutorial.In case of any problem please contact us:administration@configure-all.com

Three columns layout template created in such a way that the middle column content coming first in the HTML code. This way, Google spider, while visiting your page, will see your main content first. Then the right and the left columns are placed.

In mobile device, web page has only one column. The same web page is used for regular browsers and a mobile device. It is very important because if you create two pages with the same content, Google will punish you for duplicate content.

Download My tutorial in PDF

My tutorials are different from all other tutorials and books because I do not skip initial steps that a reader needed to start learning. Some times, you buy a book and start to read it. You understand everything from the point the author starts, but you do not know how to get to that starting point. I never miss the beginning!

<?php

&hostname = "localhost";
&dbuser = "myuser";
&dbpassword = "mypasswprd";
&dbname = "mydbname";

     &db_link=mysql_connect(&hostname, &dbuser, &dbpassword)
     or die("Unable to connect to the server!");

     mysql_select_db(&dbname)
     or die("Unable to connect to the database");

        $fields_array=array();
        $num_fields=0;
        $num_row=0;
       

        $sql="select product, price from products where catid=1 order by product ";

// find position of "FROM" in query
        $fpos=strpos($sql, 'from');

        // get string starting from the first word after "FROM"
        $strfrom=substr($sql, $fpos+5, 50);

        // Find position of the first space after the first word in the string
        $Opos=strpos($strfrom,' ');

        //Get table name. If query pull data from more then one table only first table name will be read.
        $table=substr($strfrom, 0,$Opos);

       // Get result from query
        $result=mysql_query($sql);
        $num_row=mysql_numrows($result);

        print('<html>');
        print('<head><title>');
        print('View&nbsp'.$table.'</title>');
        print('<link rel="stylesheet" href="style.css">');

        print("</head>");
        print('<body><br>');

        if($num_row >0)
        {
                //Get number of fields in query
                $num_fields=mysql_num_fields($result);

     

       # get column metadata
        $i = 0;

         //Set table width 15% for each column
        $width=15 * $num_fields;

        print('<br><table width='.$width.'% align="center"><tr>');
        print('<tr><th colspan='.$num_fields.'>View&nbsp'.$table.'</th></tr>');

         while ($i < $num_fields)
         {

          //Get fields (columns) names
        $meta = mysql_fetch_field($result);

        $fields_array[]=$meta->name;

       //Display column headers in upper case
   print('<th><b>'.strtoupper($fields_array[$i]).'</b></th>');

                $i=$i+1;
                }

        print('</tr>');
            

               //Get values for each row and column
            while($row=mysql_fetch_row($result))
            {
             print('<tr>');

                    for($i=0; $i<$num_fields; $i++)
                    {
                    //Display values for each row and column
                    print('<td>'.$row[$i].'</td>');

                    }

            print('</tr>');
            }

}

?>

Webhosting Pad 1.99/month Get 15% off on all webhosting packages! Use code 15off.

Cool Handle Buy Web Hosting Now!

http://www.justhost.com Professional Hosting from Just Host $4.95/month - Unlimited disk space and transfers and host unlimited domains I am using this host for my another web site www.best-your-trip.com

Drive more traffic to your business with Web hosting from Gate.com!

Did you find information useful? Send to your friend a link to this page

Please rate the tutorial

1 2 3 4 5 6 7 8 9 10



Comments
Register to add comments ( 5000 chars ) for display_any_table.php.
Are you human? Please select two the same numbers