Guides > Query Builder

Introducing The Query Builder Component

The Query Builder component is an excellent tool that will help you create and execute sql queries in a unique fashion way, by taking advantage of the new features that php 5.3 has to offer. The componenet is very easy to use, and can simplify the process of parsing and manipulating data in your database. The class has a set of tools that can access database tables, and process data with just 2 or 3 lines of code. The component, gives the ability to create all types of queries, and can also be used to prepare statements and execute them multiple times. The Pdo library to used to execute queries, with place holders added to values automatically, making the task of running queries a lot safer. The PtcEvent component can also be used to fire the "ptc.query" event, when queries are executed. The Query Builder is especially adviced to programmers that work on a daily basis with database queries.

Main Features:

  • Simple and intuitive sintax for building queries
  • Prepared sql statements for multiple execution
  • Support for different types of where operators
  • Can handle sql joins in a very simple way
  • Uses The PtcDebug class to log sql queries and execution timing
  • Can be used with the PtcEvent component to observe executed queries
  • Protects against sql injection by adding place holders automatically
  • Can be used with the PtcDb component to handle multiple instances
  • Adds `backticks` to columns and tables automatically

Getting Started

To get started, download the file PtcQueryBuilder.php from the repository and add the following lines of code to initialize the class with PDO support to be able to execute queries:

  1. // initializing a pdo object to run queries with the query builder
  2. $pdo_connection = 'mysql:host=localhost;dbname=some dbname;charset:uft8;'
  3. $pdo =new PDO( $pdo_connection ,'some user' ,'some pass' );
  4.     
  5. // initializing the query builder object with PDO support
  6. require_once( 'PtcQueryBuilder.php' );
  7. $qb = new PtcQueryBuilder( $pdo );
We now have an initialized intance of the query builder with PDO support to execute our queries.
Lets create a query that will retrieve all records matching a where clause, just to see the component in action:
  1. // retrieve records that match a where clause
  2. $qb->table( 'some table' )
        ->where( 'some_column' ,'=' ,'some value' )
        ->run( );
That should get you started with the Query Builder component.
For a complete list of options and examples read the full user guide.


**If you like this class, help the project to grow by rating or writing a review at one of the following sites:

⇑Back to Top⇑