ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.

Advertisements

Taking a first look at the AutoCRUD for PHP library

(Page 2 out of 5)

Setting it all up

If you want to follow along with the examples, create a database with the following structure:

CREATE TABLE `article` (
  `articleid` smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL DEFAULT '',
  `content` text,
  `datetimestamp` int(10) UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY  (`articleid`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

CREATE TABLE `article2category` (
  `article` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
  `category` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY  (`article`,`category`)
) TYPE=MyISAM;

CREATE TABLE `category` (
  `categoryid` smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` varchar(255) NOT NULL DEFAULT '',
  `parent` smallint(5) UNSIGNED DEFAULT '0',
  PRIMARY KEY  (`categoryid`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

CREATE TABLE `comment` (
  `commentid` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  `title` varchar(25) DEFAULT '',
  `author` varchar(255) DEFAULT NULL,
  `article` smallint(6) NOT NULL DEFAULT '0',
  PRIMARY KEY  (`commentid`),
  KEY `article` (`article`)
) TYPE=MyISAM AUTO_INCREMENT=1 ;

CREATE TABLE `user` (
  `userid` smallint(5) UNSIGNED NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL DEFAULT '',
  `password` varchar(255) DEFAULT '',
  PRIMARY KEY  (`userid`),
  UNIQUE KEY `username` (`username`)
) TYPE=MyISAM AUTO_INCREMENT=1;

The first step in using the AutoCRUD for PHP library is to include the 'autocrud.php' file, and creating a new instance of the AutoCRUD class. See the example below:


include ('autocrud/autocrud.php');

$crud = new AutoCRUD;

?>

Now that we've got an AutoCRUD object it's time to connect to your MySQL database. This can be done using the connect() method, see the example below:


include ('autocrud/autocrud.php');

$crud = new AutoCRUD;
$result = $crud->connect ('sa', '', 'demo');

?>

If all goes well, the connect() method will return true. But if something went wrong an AutoCRUD_Error object is returned, which is a special object to handle errors. If you want to check if an error was returned, you can use the autocrud_is_error() function, like so:


include ('autocrud/autocrud.php');

$crud = new AutoCRUD;
$result = $crud->connect ('sa', 'invalid_pass', 'demo');

if (autocrud_is_error($result) == true) {
        // error occured, find out what error
        echo $result->getCode() . ': ' . $result->getMessage();
        die();
}

// will output: no-connection: Unable to connect to MySQL server; incorrect server, username or password.

?>

Now that we have a connection with the database, it's time to create individual objects for each table. This can be done with the generate() method, like this:


include ('autocrud/autocrud.php');

$crud = new AutoCRUD;
$result = $crud->connect ('sa', '', 'demo');

if (autocrud_is_error($result) == true) {
        // error occured, find out what error
        echo $result->getCode() . ': ' . $result->getMessage();
        die();
}

$crud->generate();

?>

The generate() method will automatically inspect your database, and generate all the necessary objects. To get the structure of your database, the library will send some queries to your database. If you're concerned with performance, or if you're about to release a script for public use, you can use the alternate include_crud() method to include a separate pre-generated CRUD file. But since we're still in development mode here, we'll just stick to the generate() method. If you want to know more about using a pre-generated CRUD file, check out the documentation (once it's available).

Everything has been set up now, and the CRUD objects are ready to be used. Let's have a look at the basic CRUD functionality.

« Previous: Introduction & Overview
Next: Basic CRUD functionality »



One Response to “Taking a first look at the AutoCRUD for PHP library”

  1. Mac OS X Things » Blog Archive » PHPit - Totally PHP » Taking a first look at the AutoCRUD for PHP library Says:

    […] PHPit - Totally PHP » Taking a first look at the AutoCRUD for PHP library In this article we’ll take a look at the AutoCRUD for PHP library, which is a database abstraction library specifically for MySQL. I’ll take you through all major features of this library, and demonstrate everything with examples. […]

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Overview
  2. Setting it all up
  3. Basic CRUD functionality
  4. Selecting data
  5. Handling relationships & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file