<?php
require_once('conf_db.php');

/*Table name*/
$table_name="cds2";

/*Open db connection*/
$db->open();

/*Query for create table*/
echo "<pre>Creating table...</pre><br />";
$q="CREATE TABLE IF NOT EXISTS ".$table_name."(titel varchar(200) DEFAULT NULL,";
$q.="interpret varchar(200) DEFAULT NULL,jahr int(11) DEFAULT NULL,";
$q.="id bigint(20) unsigned NOT NULL AUTO_INCREMENT,  PRIMARY KEY (id)) ";
$q.="ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;";
if(!$db->execute($q)){
	exit();
}

/*Insert sample data*/
echo "<pre>Insert data...</pre><br />";
$q="INSERT INTO ".$table_name."(titel,interpret,jahr) VALUES('Speed of Sound','Coldplay',2005)";
$db->execute($q);

/*Show table content*/
show_cds($db,$table_name);

/*Start insert query with transaction*/
echo "<pre>Insert data with transaction...</pre><br />";
$db->execute("START TRANSACTION");
$q="INSERT INTO ".$table_name."(titel,interpret,jahr,id) VALUES('Beauty', 'Ryuichi Sakamoto', 1990, 2)";
$q.=",('Goodbye Country (Hello Nightclub)', 'Groove Armada', 2001, 3),('Glee', 'Bran Van 3000', 1997, 4);";
/*If the insert statement goes on error, I ROLLBACK transaction, otherwise I COMMIT it*/
if(!$db->execute($q)){
	$db->execute("ROLLBACK");
	echo $db->get_str_error();
}
else{
	$db->execute("COMMIT");
}
echo "<hr />";

/*Show table content*/
show_cds($db,$table_name);

/*Delete table*/
$q="DROP TABLE ".$table_name;
$db->execute($q);

/*close db connection*/
$db->close();

/*Utility Function*/
function show_cds($db,$table_name){
	$q="SELECT * FROM ".$table_name."";
	$rows=$db->query($q);
	foreach($rows as $r){
		foreach(array_keys($r) as $k){
			echo "<b>".$k.":</b> ".$r[$k]."<br />";
		}
		echo "<hr />";
	}
}
?>
