<HTML><BODY>

<?php
$table="test_table";
$dbhost = "localhost";
$dbname = "test";
$dbuser = "";
$dbpass = "";

/* Conneced to database server*/
$link = mysql_connect($dbhost, $dbuser, $dbpass)
   or die("Could not connect to mysql on $dbhost: " . mysql_error());
echo "Connected successfully to mysql on $dbhost<br>\n";

/* select database */
mysql_select_db("test") or die("Could not select database $dbname");

db_dosql("DROP TABLE IF EXISTS ".$table);
db_dosql("CREATE TABLE $table (
    subject varchar(80),
    msg varchar(255)
)");

db_addrow($table,"This is subject1", "This is message 1");
db_addrow($table,"This is subject2", "This is message 2");
db_listrows($table);
mysql_close($link);


function db_dosql($query) {
	mysql_query($query) or die("Query $query failed : ". mysql_error());
}

function db_addrow ($table,$subject,$msg){
	$query = "INSERT INTO $table (subject,msg) values ('$subject','$msg')";

    $result = mysql_query($query) or die("Query $query failed : " . mysql_error());	
}

function db_listrows ($table) {
	$query = "SELECT * FROM $table";

    $result = mysql_query($query) or die("Query $query failed : " . mysql_error());
	echo "Contents of table $table<br>\n";
	echo "<table>\n";
	while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
		echo "\t<tr>\n";
		foreach ($line as $col_value) {
			echo "\t\t<td>$col_value</td>\n";
		}
		echo "\t</tr>\n";
		}
	echo "</table>\n";

	/* Free resultset */
	mysql_free_result($result);
}

?> 
</BODY></HTML>
