Sunday, March 1, 2009

ADODB

ADODB is a database abstraction layer that allows developers to write portable code that is not tied down to one particular type of database. It also has an ActiveRecord feature which turns database rows into objects. That lets me work with object-oriented programming rather than tediously writing and rewriting MySQL queries. I've spent the last 10 days familiarizing myself with these tools and experimenting with them.

The first thing I did was research and learn to connect to my database with the tools built into PHP. I've successfully written a couple scripts that can connect to my database, create a PHP object, and perform an insert into the database, using the properties of that object as the field values for the table. One script looked like this:


class stylist{

public $first = 'not set';
public $last = 'not set';
public $email = 'not set';
public $password = 'not set';

function stylist($firstname, $lastname, $emailaddress, $pass){
$this->first = $firstname;
$this->last = $lastname;
$this->email = $emailaddress;
$this->password = $pass;

}

function insert(){
mysql_query("INSERT INTO stylist (first, last, email, password) 
VALUES ('$this->first', '$this->last', '$this->email', '$this->password')");
}
}

$con = mysql_connect("localhost","rjroth","aZsXdCf");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }
mysql_select_db("salonbook", $con);

//Create new stylist objects
$bob = new stylist('Bob', 'Vance', 'Bob@Bob', 'BV');
$jane = new stylist('Jane', 'Dance', 'Jane@Bob', 'JD');
//Call the function that executes the MySQL query
$bob->insert();
$jane->insert();

mysql_close($con);

?>


This was great, but because the insert() function uses a hard-coded MySQL query, this code would be useless if my data were moved to a different database. That's where ADODB comes in. Useful ADODB tutorials and examples were not easy to come by on the internet, but I was able to piece together a cursory understanding that allowed me to use ADODB to perform some simple scripts that can get and set data to MySQL. Here is one example of a script that SELECTS * from my "state" table and prints each element on a separate line and numbers them:


include("/Library/WebServer/Documents/adodb/adodb.inc.php");
 $db = NewADOConnection('mysql');
 $db->Connect("localhost", "rjroth", "aZsXdCf", "salonbook");
 $result = $db->Execute("SELECT * FROM state");
 if ($result === false) die("failed");  
 while (!$result->EOF) {
for ($i=0, $max=$result->FieldCount(); $i < $max; $i++)
  print $result->fields[$i].' ';
$result->MoveNext();
print "
\n";
 } 

?>


This is a step in the right direction, but it still doesn't save me much tedium as a coder and its only a small step in the direction of object-oriented coding. ADODB implements a version of ActiveRecord that can make the entire process of getting and setting data object-oriented. Unfortunately, there are even less examples of code that use this online. Here's the most helpful one I could find: 

http://phplens.com/lens/adodb/docs-active-record.htm

I tried to implement this and integrate it with my database. However, I was unable to get anything to work. When I ran the script no INSERTS would actually be made into database table, and thus far my debugging efforts have been futile. Below is the script that mirrors the example from the above link; it is designed to load a row from the "states" database into a newly created "state" object, and then as a test I want to print out the ID from the state that was just loaded:


include('/Library/WebServer/Documents/adodb/adodb.inc.php');
require_once('/Library/WebServer/Documents/adodb/adodb-active-record.php');


// create an object instance
// configure library for a MySQL connection
$db = NewADOConnection("mysql");

// open connection to database
$db->Connect("localhost", "rjroth", "aZsXdCf", "salonbook") or die("Unable to connect!");
echo 'hello world';
class state extends ADOdb_Active_Record
{
var $_table = 'state';
}

$state = new state();
$state->load("id=5");
echo $state->id;

?> 

Unfortunately, I can't get this to work so far. Hopefully, with help from Jeff, I will turn the corner this week and go on to build all the objects I will need to represent my database.

No comments:

Post a Comment