WARNING: Your browser, IE6, released in 2001 and superseded by IE7 in 2006, is out of date. I advise that you either upgrade to IE7 or switch to a more standards-compliant browser.

Archive for the ‘PHP’ Category.

PHP Database Library

For a while now, I’ve been working on a PHP DB wrapper class. The whole point of it is to make writing the same old queries over and over again a little bit easier to manage. I’ve noticed that I haven’t had to edit it over the past few months, and I’ve implemented it at work without issue on many occasions, so I figure it’s time to share it with the world.

To use it, first create an instance of the object:

$db = new db("user", "pass", "db", "host");

And once you have it, you can perform various types of queries.

For selects that will return multiple rows, use the following syntax:

$rows = $db->q("SELECT * FROM tbSomeTable;");

$rows will then either be false (which means the record had an error or returned 0 records) or an array of row objects:

foreach($rows as $row){
    print($row->someField);
}

For single-row queries (such as those with LIMIT 1 or using a COUNT or similar function), use sq:

$row = $db->sq("SELECT COUNT(*) AS intCount FROM tbSomeTable;");

$row is then an object representing a single row of the resultset. If the query returns multiple rows, only the first row is used.

For insert, update, delete, and other queries, $db->q is used.

An advantage to my wrapper is the printf-like auto-escaping, allowing you to easily use user input:

$rows = $db->q("SELECT * FROM tbUsers WHERE User='%s' AND Pass='%s';", $_POST["user"], $_POST["pass"]);

The %s are then replaced with the corresponding argument and automatically stripped using mysqli’s strip function.

Here’s a short snippet tying everything together showing how quick and easy it is to write a DB resultset iteration:
if(($rows = $db->q("SELECT * FROM tbUsers WHERE intUserType='%s';", $_GET["intUserType"])) !== false){
    foreach($rows as $row){
        print($row->strUsername . “<br />”);
    }
}else{
    print(”No records found!”);
}

Debugging can be achieved via $db->messages();

It also has support for transactions (commit, rollback), documented in the readme

If you want to give it a try, you can download it here

If you have any questions or suggestions, leave them in my comments!

Updates

I’m currently working on a rapid development framework, which combines the MVC pattern, a Data Object, a DB Wrapper, AJAX helpers, and many other things implemented as helpers, rather than as the typical framework approach, hindering development more than speeding things up. I like to have more control over my apps than CakePHP offers, so I’ve taken it upon myself to write my own alternative. It’s certainly not as refined, but that’ll just take time. Also, documentation will actually be beneficial for people unlike CakePHP.

I’m in my new apartment, all set up except for a few things. I’m going to be buying some wood and building a desk for my living room, and I’ve ordered some LEDs and with the help of my friend Mike we’re gonna set up my living room with some low-power bright LEDs. I’m thinking of putting them up behind some wax paper, to sort of diffuse the light and soften it. Next up is to find somebody who can paint, especially on a large scale, because I’d like to be able to have a sweet mural/painting on my wall.

My friend Josh and I have decided to use Adobe AIR to develop our own todo list with built in timer to keep track of hours spent on projects at work.

I’ve also decided I’m going to build my own desk, and I’ve already got the designs. It’s going to be 30 inches deep, and about 4 feet wide. The back half of the desk will have an elevated platform for 4 monitors to sit on.

Lastly, I’ve begun twittering regularly, so check me out and follow me if you like: http://twitter.com/dmsuperman

Alright, I’m off for now but I’ll be coming back soon with more updates, and eventually the framework will be posted for public use.

Twitter