The FlatSQL package is a small but very poweful set of classes for database like access to text “flat files”. It provides equivalents to many of the common SQL commands. Below is an extended example of how to use Flatfile and the related classes. The example is a table that holds ‘posts’ (such as a message on a message board), each with an ID, the title, the date, the author and the text of the post. This package does not require MySQL or any database engine to run, it is entirely standalone and requires just PHP only to run. Setup
Now we need to create the database object and set it up. In this example we are using a database called CMS. The database is created automatically if it does not exist. require_once(‘ffdb/dbase.php’); $db = new Flatsql(); $db->query(“use dbase cms”);
INSERT
To insert a row, use the following sql statements. An autoincrement ID is generated by default. And oh, there is no create table function, table is created automatically if it does not already exist. The return value is the newly generated ID. Notice that the first field called uid is the auto-generated I.D - this is required be default. $db->query(“insert into users(uid,first,last,city,phone) value(Null,’Anthony’,’Ogundipe’,’Lagos’,’08027034534’)”); echo $db->newId; //latest id SELECT
If you are familiar with MySQL or sql in general, then the commands below will not be new to you. $row=$db->query(“select * from users”); //select all the data available $row=$db->query(“select first,last from users”); //select first and lastname only $row=$db->query(“select last,phone,first from users”); $row=$db->query(“select first,last from users where city=’Lagos’ order by uid asc INTEGER_COMPARISON”); //ordering data $row=$db->query(“select first,last from users where city=’Lagos’ order by first asc limit 1”); //limiting data returned $row=$db->query(“select first,last from users where city=’Egbe’”); //where clause $row=$db->query(“select first,last from users where uid=’2’”); $row=$db->query(“select first,last from users where uid>’2’ and first=’Okon’”); //multiple conditions $row=$db->query(“select first,last from users where city=’Lagos’ and phone=’070603223456’”); $row=$db->query(“select first,last from users where city=’Lagos’ and phone=’08027034534’ ”); $row=$db->query(“select first,last from users where city=’Lagos’ order by phone asc”); $row=$db->query(“select first,last from users where city=’Lagos’ and phone=’08027034534’ order by phone asc”); The result will be an array of arrays. You can loop through the associative arrays to get the individual data. UPDATE
To update records stored in tables, you can use regular MySQL statements. $db->query(“update users set last=’White’,city=’Broad Street’ where uid<’3’”); $db->query(“update users set last=’White’ “); //this will update the entire column DELETE RECORDS
MySQL delete statements are useful here also. $db->query(“delete from users where uid=’1’”); //delete conditionally $db->query(“delete from users”); //delete entire rows - truncate DELETE TABLE
To delete an entire table and all the data it contains. $db->query(“drop table users”); TRUNCATE TABLE
To delete all the records stored inside a table. $db->query(“truncate table users”); COMBINING WITH FLAT FILE PACKAGE
You can use flat file commands concurrently as shown below. However, flatfile discussion is outside the scope of this tutorial. $db = new Flatsql(); $db->query(“use dbase cms”); $aSingleRow = $db->selectUnique(‘cms.txt’, 0, ‘2’); var_dump($aSingleRow);
This is not a replacement for mysql o. I just made this just for the fun of it. . . . Aye, you can find it on www.facebook.com/ |