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 3 out of 5)

Basic CRUD functionality

Inserting new records usually involve tedious SQL queries, but not with the AutoCRUD for PHP library. All it takes is the following code:


// ...
// setup AutoCRUD
// ...

$new_row = array('title' => 'My article', 'content' => 'Article content goes here', 'datetimestamp' => time());
$result = $crud->article->insert ($new_row);

echo 'Row inserted';
?>

Let's analyze this example. As you can see, the generate() method has created a property called 'article', which is an object that points to the article table. We use the insert() method on the article object to insert a new record, and we pass an associative array as the first argument. That's all it takes really, and there should now be a new record in the article table.

In this case nothing went wrong, because the row data we entered was correct. But what if we left the title out, which we can't do because the title field is declared NOT NULL. Or what if we entered some text in the datetimestamp field, which only accepts non-numeric data?

Like the connect() method, the insert() method will return an error object on failure, and it can be handled in the same way, like this:

// ...
// setup AutoCRUD
// ...

$new_row = array('content' => 'Article content goes here', 'datetimestamp' => time());
$result = $crud->article->insert ($new_row);

// we left out the title, so error should be returned

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

// will output: missing-field: Missing field in data: `title`

?>

If everything goes okay, the insert() method will return the primary key of the row that was inserted.

There's also an update() method to update a record. It's used in a similar way to the insert() method, but takes a second optional argument to pass the primary key of the record you want to update:

// ...
// setup AutoCRUD
// ...

$row = array('title' => 'New title', 'content' => 'New Article content goes here', 'datetimestamp' => time());
$result = $crud->article->update ($row, 1);

// we left out the title, so error should be returned

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

echo 'Record updated';

?>

Like the insert() method, it also returns an error object on failure. It will return true on success.

One thing to keep in mind with the update() method is that you don't need to specify all the fields. It's now possible to leave out the title field, since a title has been set on the article already. It will still reject an empty title though.

If you leave out the second argument the whole table will be updated, so be very careful about this, since you probably don't want this.

It's also possible to specify your own custom WHERE clause, instead of passing the primary key. See the example below:


$row = array('title' => 'New title 2', 'content' => 'New Article content goes here', 'datetimestamp' => time());

$id = '1';
$crud->article->where = "articleid = " . $crud->quote($id);
$result = $crud->article->update ($row);

// we left out the title, so error should be returned

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

echo 'Record updated';

?>

As you can see we set the 'where' property on the article object to specify a custom WHERE clause, and we also use the quote() method to make sure the values that are passed are properly escaped. This is very important, since it protects us from SQL injections.

Finally, there's also a delete() method, which is used to delete a record, and takes only one optional argument; the primary key of the record you want to delete. It's used like this:


// ...
// setup AutoCRUD
// ...

$crud->article->delete(1);
echo 'Record delete';

?>

Like the update() method it's possible to omit the primary key, but this is definitely not recommended since it will delete ALL the records in the table. You can also specify a custom WHERE clause using the 'where' property.

Now that we've seen how to modify data on the table, let's look at selecting data.

« Previous: Setting it all up
Next: Selecting data »



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