Rss Feed
Tweeter button
Facebook button
Technorati button
Reddit button
Myspace button
Linkedin button
Webonews button
Delicious button
Digg button
Flickr button
Stumbleupon button
Newsvine button
Youtube button

Notes:

  • The site will be morphing over the next little while.
  • I am having some issues with tabs/spaces in the Python code. Sometimes they are correct, sometimes they get eaten. I am trying to figure it out.

Please feel free to leave a comment on a post if it interests you or if you have questions

Jan 082010

I found this code on the internet. This is a nice bit of code to show an arbitrary table with headers and all the entries. There is probably lots more one could do with it, but it works well for general testing and is completely independent of what is in the table.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
function display($table)
{
    $db_host = 'localhost';
    $db_user = '<userid here>';
    $db_pwd = '<password here>';
 
    $database = '<sql database here>';
 
    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";
    }
        echo "</table>";
    mysql_free_result($result);
}
© 2010 Math, Computing and Research Please leave a comment or contact me craig@mri.brechmos.org if you have any questions. Suffusion WordPress theme by Sayontan Sinha