Making database queries

PHPDevShell supports from legacy PHP query functions to straight forward query methods moving on to more advanced models and ORM. You can obviously use whatever you want or write your own plugin that shares the way you like to do queries.

PHPDevShell will maintain your connection to the database, so you will be ready to make a query at any time. It does not matter if you just use php database functions or PHPDevShell methods or ORM, the query is ready to be made.

PHPDevShell main query system only supports MySQL at this stage. There are several reasons for this, one of the most prominent reasons are performance and complications that goes with a DB layer. You are still free to do queries to other database systems, however, the core will still run MySQL for now. We are following technology closely and will most definitely support multiple databases without performance interference.

So in this example we will create a single node, point a menu to use it and write the examples below. So lets create a file under plugins/MyPlugin/example-query.php, and create a menu item for it so we can access it.

Simple queries using PHP

Here we will focus on just using PHP to do a query, the way you are used to:

<?php
    // Hello world heading.
    echo "

Hello, world!

"; $query = mysql_query('SELECT * FROM pds_core_users WHERE user_id = 1'); $query = mysql_fetch_array($query); ?>

Simple queries using PHPDevShell

In general it is not a good idea to use PHP database function directly, so using the PHPDevShell layer we can do a simple query like this:

<?php
    // Hello world heading.
    echo "

Hello, world!

"; $query = $this->db->newQuery('SELECT * FROM pds_core_users WHERE user_id = 1'); ?>

Advanced queries with models

If you are looking at making serious applications you would want to use the PHPDevShell model structure that falls in with MVC. We will not cover the full topic here as it is quite advanced and needs a documentation page of its own. However in the end, a query will look like this;

<?php
    // Hello world heading.
    echo "

Hello, world!

"; $user_name = $this->db->invokeQuery('getUserName'); echo $user_name; ?>

For more information on advanced queries, have a look at MVC

Using ORM for queries

Then we have full support for ORM using the very well written light RedbeanORM. This system is brilliant for medium complexity websites. You will need to call the ORM plugin for this one, to do a query simple do;

<?php
    // Hello world heading.
    echo "

Hello, world!

"; // Call ORM plugin. $this->factory('orm'); // Do the query $books = R::find('example_book'); ?>

The advantage of using an ORM is that you will never need to look at the database, you handle data as code and the backend takes care of the rest. This also works well with forms and data imports. Database structure is created dynamically as you add data to columns etc. Look at the RedBean documentation to learn more about its features.

Conclusion

Lightly touching a very important subject the idea is to give you ideas on how you could do queries. Follow one or all methods, in later topics data is discussed in more detail.

PHPDevShell © 2010 - All rights reserved.