Starting with SQLITE WITH PHP
- January 26th, 2010
- Write comment
First of all you need to make sure that the extension of sqlite is working through the settings in php.ini
Assuming its working, I will make it brief first create a file names create_database.php
And add the following to it
$db= new SQLiteDatabase("db.sqlite");
$db->query("
BEGIN;
CREATE TABLE users (id INTEGER UNSIGNED PRIMARY KEY,
name CHAR(255),
email CHAR(255));
INSERT INTO users (id,name,email) VALUES (NULL,'User1','user1@domain.com');
insert into users (id,name,email) values (NULL, 'user2','user2@lamis.com');
insert into users (id,name,email) values (NULL, 'user3','user3@lamis.com');
COMMIT;
");
This will create a new SQLiteDatabase and a table called users
now create another file call it test_result.php
and add the following to it
$db= new SQLiteDatabase("db.sqlite");
$result = $db->query("Select * from users");
echo "<br><br>++++++ POINTER WAY +++++++<br><br>";
while($result->valid())
{
print_r($result->current());
echo "<br>";
$result->next();
}
echo "<br><br>++++++ ASSOCIATIVE WITH FETCH+++++++<br><br>";
$result = $db->query("Select * from users");
while($row = $result->fetch(SQLITE_ASSOC))
{
print_r($row);
echo "<br>";
}
echo "<br><br>++++++ NUM WITH FETCH+++++++<br><br>";
$result = $db->query("Select * from users");
while($row = $result->fetch(SQLITE_NUM))
{
print_r($row);
echo "<br>";
}
echo "<br><br>++++++ FETCH ALL ++++++<br><br>";
$result = $db->query("Select * from users");
$rows= $result->fetchAll();
foreach($rows as $row)
{
print_r($row);
echo "<br>";
}
echo "<br><br>++++++ FETCH Object ++++++<br><br>";
$result = $db->query("Select * from users");
while($row = $result->fetchObject())
{
print_r($row);
echo "<br> you can call any like this \$row->id<br><br><br>";
}