Welcome, Guest

Please login or register

TUTORIALS SUBMENU

PHOTOSHOP    FLASH    ILLUSTRATOR    BLENDER    CINEMA 4D    WEB-CODING    [SUBMIT]

Related Links

Dumping mySQL Tables to HTML

pages (2): [1] 2


In this tutorial we are going to expose basic mySQL queries, and how they can be used to print all data from a mySQL table into an HTML table.

This might be useful if you want to display all the data for review reasons. A couple of things to take into consideration however when following this tutorial; For one thing, this tutorial will assume that the mySQL table we will print does not contain a lot of records. Obviously, if the table contains thousands of records, the script will take a long time to process and the rendered HTML page itself will be enormous. You should only use this tutorial to get a basic understanding of how to print the data, and they modify the code to suit your needs.

Secondly, the code shown does not require you to know the mySQL table field names. It is dynamic, meaning it only requires a mySQL table and then the code does everything else automatically. This makes the code more portable. Finally, you should have a common knowledge of working with PHP and mySQL in order to stay up to speed when reading this tutorial.

So, let's get started!  We will first display the entire code, then break it down and explain each section, and how it works...



The Code

<?php

// Open Connection To mySQL.
// Replace 'username' and 'password' with appropiate login values for
// your server.

$mysqlLink mysql_connect'localhost' 'username' 'password' );

// Select mySQL Database.
// Replace 'database' with appropriate database for your server.

mysql_select_db'database' $mysqlLink );

// Select mySQL Table.
// Replace '$table' with appropriate table for your server.

$table 'table';

/**
 * Print Table.
 *
 * Print Table consists of selecting the mySQL table, determine
 * if there is data in it, selecting
 * that data, then actually formatting it in an HTML table.
 *
 * If there is no data, then appropriate messages are displayed.
 */

// Check If Query Was A Success.

if( $result mysql_query('SELECT * FROM ' $table ) )
{
    
// Start Table.

    
echo '<table border="1">';

    
// Get Number Of Fields In Table.

    
$fieldCount mysql_num_fields$result );

    
// Start First Row.

    
echo "<tr>";

    
// Loop Through Each Field And Display It In Row.
     
    
for( $i 0$i $fieldCount$i++ ) {
        echo 
'<td><strong>' mysql_field_name$result$i ) . '</strong></td>';
    }

    
// Close First Row.

    
echo "</tr>";
    
    
// Check If There Is Data In Table.
    
    
if( mysql_num_rows$result ) )
    {
        
// Loop Through Each Row And Display It.
        
        
while( $row mysql_fetch_row$result ) )
        {
            
// Start Row.
            
            
echo '<tr>';
            
            
// Loop Through Each Field For Current Row.
            
            
for( $i 0$i $fieldCount$i++ ) {
                echo 
'<td>' $row[$i] . '</td>';
            }
            
            
// Close Row.
            
            
echo '</tr>';
        }
    }
    
    
// No Data In Table.
    
  
else {
   echo 
'<tr><td colspan="' $fieldCount '">No rows selected...</td></tr>';
  }
    
    
// Close Table.
    
    
echo '</table>';
}

// Invalid Query Or Un-Success.

else {
    echo 
'Query Error: ' mysql_error( );
}

// Close mySQL Connection.

mysql_close$mysqlLink );

?>

That was a lot of code!  Its simple but rather long. So, let's break down the code into smaller parts and explain what each section does:

- Tutorial written by Limitless

Pages (2): [1] 2 Next>
Automatic Translations: Translate Into French Translate Into German Translate Into Italian Translate Into Spanish Translate Into Portuguese

Last 5 User Comments


There are no comments for this tutorial yet.
You can place a comment by clicking here.
Amazing Font Pack!

Featured Tutorialsmore

Sunbursts
Sunbursts
- Adobe Illustrator -
Xpresso Tutorial -...
Xpresso Tutorial -...
- Maxon Cinema 4D -
Razor Stryke Logo
Razor Stryke Logo
- Adobe Photoshop -
Pixel Stretch Swirl
Pixel Stretch Swirl
- Adobe Photoshop -
Membership

Username:
Password:  
Remember Me

Lost Password? || Register

Related Links



Special Options
Printer Friendly Version
Forum Threads

 Deactivate Account
Author: jerinian
Posted: Oct 02nd, 11:16am
Activity: 1 replies, 887 views
 changes....
Author: supertackyman
Posted: Sep 12th, 2:56am
Activity: 2 replies, 1052 views
Back again and with free webhosting :)
Author: ngz
Posted: Aug 14th, 3:50pm
Activity: 0 replies, 1054 views
Cartoon Crab 6 Legs Walk Run created in Blender
Author: patricia3d
Posted: Jun 19th, 12:58pm
Activity: 0 replies, 1935 views
HTML Form Post Array to PHP
Author: Space Cowboy
Posted: May 25th, 2:18pm
Activity: 0 replies, 1832 views
My blog where i create Digi Scrapbook
Author: claudya07
Posted: May 11th, 2:33pm
Activity: 0 replies, 14441 views
Blood Dripping from Letters
Author: patricia3d
Posted: Apr 05th, 3:37am
Activity: 0 replies, 2751 views
A New Designer has joined the ranks
Author: skates1
Posted: Mar 28th, 2:19pm
Activity: 2 replies, 2772 views
The hole in Photoshop
Author: Mars
Posted: Feb 13th, 9:28pm
Activity: 2 replies, 3439 views
Colour Swatch
Author: ebz7350
Posted: Jan 15th, 11:18am
Activity: 0 replies, 2356 views
 BioRUST Forums - Reply to Topic
Author: inonShozy
Posted: Jan 11th, 11:32am
Activity: 8 replies, 2499 views
 Version 2 of my portfolio site.
Author: andrewnleon
Posted: Jan 08th, 6:18am
Activity: 6 replies, 2798 views
Forum Threads

--- Site Resources ---
Total Tutorials:212
Total Downloads:    441
Total Fonts:    4673