<html>
<body>
<?php
	echo ('SQLite Version : '.sqlite_libversion().'<br />');
	
	// creat DB 
	
	$sqlDB = '/tmp/test.db';
	if(!($db = sqlite_open($sqlDB, 0666, $err)) )
	  die($err);
	  
	$sql ="create table 
	       users
		   (
		     name     STRING,
		     text     STRING,
		     add      String
		   )";
	if(!sqlite_query($sql,$db) )
	 	die(sqlite_last_error($db));
	echo ("created data base $sqlDB successfully.");	
	sqlite_close($db);
?>	
<h1>initialize data base</h1>
<?php

$sqlDB = '/tmp/test.db';
if(!$db = sqlite_open($sqlDB, 0666, $err))
{
	die("Error: $err");
}
$users = array(
	array(
			'name'=>'jbloggs',
			'text'=> 'hello',
			'add'=>'world'
		 ),
	array(
			'name'=>'this',
			'text'=> 'is ',
			'add'=>'an example'
		 ),
	array(
			'name'=>'just',
			'text'=> 'another',
			'zusatz'=>'example'
		 ),
	array(
			'name'=>'somebody',
			'text'=> 'has been',
			'zusatz'=>'here'
		 ),
	array(
			'name'=>'forget',
			'text'=> 'all',
			'zusatz'=>'those rubbish'
		 ),
	array(
			'name'=>'Digi',
			'text'=> 'rulz',
			'zusatz'=>'or whatever'
		) );
$result = sqlite_query($db, "SELECT * FROM users");
$num = sqlite_num_rows($result);
if($num == 0)
{
	foreach($users as $user)
	{
		$sql = "INSERT INTO users (name, text, add) 
				VALUES
				(
				    '{$user['name']}',
					'{$user['text']}',
					'{$user['add']}'
					)";
	
		echo "$sql<br>";
		if(!sqlite_query($db, $sql))
		{
			die("Error: " . sqlite_last_error($db). ':' .
		                 sqlite_error_string(sqlite_last_error($db)) );
		}
	}
	echo 'some db entries created';
}
else
{
	echo ' db was non empty';
}
sqlite_close($db);

?>

<h1>data base</h1>
<?php

if(!$db = sqlite_open($sqlDB,0666, $err))
{
	die("Error: $err");
}
	
$sql = "SELECT * FROM users";
if(!$result = sqlite_query($db, $sql))
{
	die(sqlite_last_error($db).':'.
	    sqlite_error_string(sqlite_last_error($db)) );
}
echo ('<h2>data base output</h2>');
echo "<table border ='1'>";
while($row = sqlite_fetch_array($result, SQLITE_ASSOC))
{
	echo "<tr> <td>" ;
	echo "{$row['name']} </td> <td>{$row['text']} </td>
	     <td> {$row['add']} </td></tr>";
}
echo "</table>";
sqlite_close($db);



?>
</body>
</html>

