Read + Write + Report
Home | Start a blog | About Orble | FAQ | Blogs | Writers | Paid | My Orble | Login

Display MySQL output in tables

December 21st 2010 08:31
Below are functions in PHP/MySQL to use for this example:
  1. mysql_connect - connects to MySQL server
  2.  mysql_select_db - select database
  3.  mysql_query - send query
  4.  mysql_fetch_row - get current row from result table
  5.  mysql_num_fields - get number of fields
  6.  mysql_fetch_field - get field information
  7.  mysql_free_result - free result table from memory
<html><head><title>MySQL Table Example</title></head><body>
<?php
$db_host = 'localhost';
$db_user = 'root';
$db_pwd = 'mypasswd';

$database = 'jiansen_db';
$table = 'user_table';

if (!mysql_connect($db_host, $db_user, $db_pwd))
    die("Can't connect to database");

if (!mysql_select_db($database))
    die("Can't select database");

// sending query
$result = mysql_query("SELECT * FROM {$table}");
if (!$result) {
    die("Query to show fields from table failed");
}

$fields_num = mysql_num_fields($result);

echo "<h1>Table: {$table}</h1>";
echo "<table border='1'><tr>";
// printing table headers
 for($i=0; $i<$fields_num; $i  )
{
    $field = mysql_fetch_field($result);
    echo "<td>{$field->name}</td>";
}
echo "</tr>\n";
// printing table rows
while($row = mysql_fetch_row($result))
{
    echo "<tr>";

    // $row is array... foreach( .. ) puts every element
    // of $row to $cell variable
    foreach($row as $cell)
        echo "<td>$cell</td>";

    echo "</tr>\n";
}
mysql_free_result($result);
?>
</body></html>

17
Vote
Add To: del.icio.us Digg Furl Spurl.net StumbleUpon Yahoo


   
subscribe to this blog 


   

   

   

Add A Comment

To create a fully formatted comment please click here.


CLICK HERE TO LOGIN | CLICK HERE TO REGISTER

Name or Orble Tag
Home Page (optional)
Comments
Bold Italic Underline Strikethrough Separator Left Center Right Separator Quote Insert Link Insert Email
Notify me of replies
Your Email Address
(optional)
(required for reply notification)
Submit
More Posts
1 Posts
1 Posts
1 Posts
106 Posts dating from May 2008
Email Subscription
Receive e-mail notifications of new posts on this blog:
0

Jiansen Lu's Blogs

1570 Vote(s)
0 Comment(s)
37 Post(s)
1076 Vote(s)
0 Comment(s)
20 Post(s)
5628 Vote(s)
1 Comment(s)
164 Post(s)
5740 Vote(s)
51 Comment(s)
95 Post(s)
Moderated by Jiansen Lu
Copyright © 2012 On Topic Media PTY LTD. All Rights Reserved. Design by Vimu.com.
On Topic Media ZPages: Sydney |  Melbourne |  Brisbane |  London |  Birmingham |  Leeds     [ Advertise ] [ Contact Us ] [ Privacy Policy ]