Location: PHPKode > scripts > Sqlitei > sqlitei/Sqlitei_Example.php
<?php // 5 
// EXAMPLE FILE for Sqlitei.class.php
// requires the class file
require( 'Sqlitei.class.php' );

// try all these things ...
try {
	// new Sqlitei instance
	$connect = &new Sqlitei( 'test.sqlitei' );
	
	// Uncomment if this is at least second time you're testing this file
        /*
		// Dopping table ...
		$stmt = $connect->prepare("DROP TABLE test");
                // execute drop
		$stmt->execute();
                // close this prepared query
		$stmt->close();
	// */
	
	// table creation, prepare the query
	$stmt = $connect->prepare("CREATE TABLE test ( id INTEGER PRIMARY KEY, something VARCHAR(255) )");
	// simple error management with try / catch sintax
	try {
		// execute the prepared query
		$stmt->execute();
		// close this prepared query
		$stmt->close();
	}
	catch( Exception $e ) {
		// if somethong on query was wrong, die with a message
		die( $e->getMessage() );
	}
	
	// new prepared query, this time with 2 variables
	$stmt = $connect->prepare("INSERT INTO test VALUES( ?, ? )");
	// try to do queries ...
	try {
		// first table id, start from 1
		$id = 1;
		// multiple queries generation
		for( $a = 0; $a < 100; $a++ ) {
			// foreach $a insert 2 rows to test slashes on sqlite ...
			$something = 'test number '.( $a + 1 ).'a with "this" and \'that\' !';
			// send parameters to use for this query
                        // i = integer value
                        // s = not int, not double, not bool but everything else ( a string for example )
			$stmt->bind_param( 'is', $id++, $something );
			
			// and again, with differents slashes
			$something = "test number ".( $a + 1 )."b with \"this\" and 'that' !";
			$stmt->bind_param( 'is', $id++, $something );
		}
		// execute all prepared queries
                // NOTE: with multiple queries this class automatically uses a TRANSACTION
                // then if there is an error, its ROLLBACKs queries
		$stmt->execute();
		// close all these queries ...
		$stmt->close();
	}
	// ... or give me the error
	catch( Exception $e ) {
		die( $e->getMessage() );
	}
	
	// prepare another query, this time with a SELECT statement
	$stmt = $connect->prepare("SELECT * FROM test LIMIT 0, ?");
	// try my operations ...
	try {
		// limit 0, how many ??? ... 
		$stmt->bind_param( 'i', 10 );
		
		// really impossible for me to clone the MySQLI bind_result method
                // ... it's something like that but you can't send directly undefined variables ...
                // then send reult names, it's simple too
		$stmt->bind_result( 'id', 'text' );
		// execute this query
		$stmt->execute();
		// as MySQLI does, you can use fetch() method too ...
		while( $stmt->fetch() ) {
			// then to use binded results, just call this object with those names !
			echo 'ID: '.$stmt->id.'<br />'; // the id
			echo "TEXT: {$stmt->text}<br />"; // and the text value !
			echo '<hr />';
		}
		// and close this query
		$stmt->close();
	}
	// ... or tell me the error!
	catch( Exception $e ) {
		die( $e->getMessage() );
	}
	
	// however, you can use all default SQLiteDatabase class methods, isn't cool ?
        // just query !
	$query = $connect->query( 'SELECT * FROM test' );
	// for example, the number of rows ...
	echo 'Total Rows = '.$query->numRows();
	
	// close database connection, resets all and unset internal SQLiteDatabase class
	$connect->close();
}
// or tell me what's wrong
catch( Exception $e ) {
	die( $e->getMessage() );
}
?>
Return current item: Sqlitei