mirror of
https://github.com/jimeh/jimeh.me-v3.0.git
synced 2026-02-19 05:46:40 +00:00
new post: LiteMySQL: ActiveRecord's Little Brother
This commit is contained in:
@@ -1,18 +0,0 @@
|
||||
---
|
||||
layout: post
|
||||
title: "LiteMySQL: ActiveRecord's Little Brother"
|
||||
categories: [technology, database, php]
|
||||
---
|
||||
|
||||
Ever needed a quick and lightweight MySQL library for PHP? Pulling out a full ORM is just overkill, but writing the PHP code needed to connect to the server, run a query, and process the results is a lot of hassle? I thought so. I've been there too.
|
||||
|
||||
So what if you could do something like this:
|
||||
|
||||
{% highlight php %}
|
||||
<?php
|
||||
$sql = new litemysql('host', 'username', 'password', 'testdb', 'books');
|
||||
$books = $sql->find_all(array("author" => "John Twelve Hawks"));
|
||||
?>
|
||||
{% endhighlight %}
|
||||
|
||||
The above code would require about 8-10 lines of PHP code if you went barebones. I'm not gonna show a barebones example, cause frankly I'm even now too lazy to write such code.
|
||||
@@ -0,0 +1,52 @@
|
||||
---
|
||||
layout: post
|
||||
title: "LiteMySQL: ActiveRecord's Little Brother"
|
||||
categories: [technology, web, database, php]
|
||||
---
|
||||
|
||||
Ever needed a quick and lightweight MySQL PHP library for some small single/multi page project? Pulling out a full ORM is just overkill, but writing the PHP code needed to connect to the server, run a query, and process the results is a lot of hassle? I thought so. I've been there too.
|
||||
|
||||
So what if you could do something like this:
|
||||
|
||||
{% highlight php %}
|
||||
<?php
|
||||
$sql = new litemysql('host', 'username', 'password', 'testdb', 'books');
|
||||
$books = $sql->find_all(array("author" => "John Twelve Hawks"));
|
||||
?>
|
||||
{% endhighlight %}
|
||||
|
||||
Rather than something like this:
|
||||
|
||||
{% highlight php %}
|
||||
<?php
|
||||
$db = mysql_connect("host", "username", "password");
|
||||
if (!$db) die("Could not connect: " . mysql_error());
|
||||
$db_selected = mysql_select_db("testdb", $link);
|
||||
if (!$db_selected) die("Can't use testdb : " . mysql_error());
|
||||
$result = mysql_query("SELECT * FROM `books` WHERE `author` = 'John Twelve Hawks';");
|
||||
$books = array();
|
||||
while ($row = mysql_fetch_assoc($result)) {
|
||||
$books[] = $row;
|
||||
}
|
||||
?>
|
||||
{% endhighlight %}
|
||||
|
||||
About two years ago, I found myself in need of just such a small, lightweight library. There were full ORMs like [ADOdb][] and others available, but they were serious overkill for the kind of simple stuff I needed. After some googling, I noticed there didn't seem to be any in-between libraries. Either you had to go for a whole jumbo jet, or start folding your own paper airplanes, and I didn't like either option. I just wanted to grab existing paper airplanes, and start throwing them in the direction I needed them to go.
|
||||
|
||||
So in true geek fashion when you want something ready-made to make your life easy, I ended up building my own such library. Spending an order of magnitude more time on the MySQL connection part of the project than I would have needed if I'd just done it the ugly way as the second code example above.
|
||||
|
||||
And that's how I started building [LiteMySQL][]. Rather than building it from the ground up though, I started by lifting the essential parts from the full ORM/ActiveRecord implementation I'd written for [Zynapse][], and mainly just wrote glue-code to make it a feature-complete library.
|
||||
|
||||
After close to a year of being ignored, I recently spent an afternoon fixing some long-standing bugs, migrating to [GitHub][], and writing some decent documentation for the project.
|
||||
|
||||
You can read more, and download the library [here][litemysql].
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
[zynapse]: http://github.com/jimeh/zynapse
|
||||
[adodb]: http://adodb.sourceforge.net/
|
||||
[litemysql]: http://github.com/jimeh/litemysql
|
||||
[github]: http://github.com/
|
||||
Reference in New Issue
Block a user