PHP Classes

File: example

Recommend this page to a friend!
  Packages of a77icu5   Easy Query   example   Download  
File: example
Role: Example script
Content type: text/plain
Description: Example of the class
Class: Easy Query
Execute common SQL queries from parameter lists
Author: By
Last change: New samples using chaining =D
Date: 16 years ago
Size: 3,909 bytes
 

 

Contents

Class file image Download
<?php
require_once    'EasyQuery.php';
$db = new EasyQuery;

/**
* define values to return, if params is null then return all the
* values of the table else just return the values espeficied
*/
$db->values();
display($db->get('easyquery'));
echo $db->myQuery();

/*using chaining*/
display($db->values()->get('easyquery'));
echo $db->myQuery();

/**
* return records especified by the where condition, when the records to
* return is a single return, then the second param of get() will be TRUE
* 
* note: you can make multiple calls to where() method to make AND conditions
*/
$db->values();
$db->where('id_user',    1);
display($db->get('easyquery', TRUE));
echo $db->myQuery();

/*using chaining*/
display($db->values()->where('id_user', 1)->get('easyquery', TRUE));
echo $db->myQuery();

/**
* returns a range of records
* 
* this method will set the offset to second position and return three records
* this method can help you when you're paginating records
*/
$db->values();
$db->limit(2, 3);
display($db->get('easyquery'));

/*using chaining*/
display($db->values()->limit(2, 3)->get('easyquery'));

/**
* custom query
* 
* use the setQuery method when you can't make the query with the methods of the
* class fetchQuery had a boolean param to specified singlerow
*/
$db->setQuery('select * from easyquery where id_user <> 1');
$db->execute();
display($db->fetchQuery(FALSE));
echo $db->myQuery();

/**
* insert records
* 
* you can also use insertId(), this method will retur the id generated by the last 
* insert query
*/
$data = array('user_name'    =>    'username',
         'user_password'    =>    md5('password'),
        'user_mail'    =>    'user7@server.com');
$db->insert('easyquery', $data);
echo $db->myQuery();

/**
* delete records 
* 
* you can also use rowsAfected() at the end to display the rows affected by the query
*/
$db->where('id_user',    1);
$db->limit(1);
$db->delete('easyquery');
echo $db->myQuery();

/*using chaining*/
$db->where('id_user', 1)->limit(1)->delete('easyquery');

/**
* update records 
* 
* you can also use rowsAfected() at the end to display the rows affected by the query
*/
$data = array('user_name'    =>    'funny name');
$db->where('id_user',    2);
$db->limit(1);
$db->update('easyquery', $data);
echo $db->myQuery();

/*using chaining methods*/
$db->where('id_user', 2)->limit(1)->update('easyquery', $data);

/**
* count records
* 
* numRows() will count the records of the las query executed
*/
$db->values();
$records = $db->get('easyquery');
echo $db->numRows();

/*using chaining methods*/
$records = $db->values()->get('easyquery');
echo $db->numRows();

/**
* check if exists records in a table
*/
if ($db->haveRows('easyquery')) {
    echo 'Table is not empty';
} else {
    echo 'table is empty';
}

/**
* function to display the result of the query
*/
function display($data) {
    echo '<pre>';
    print_r($data);
    echo '</pre>';
}
/*
+ __construct()                            :void
+ connect()                                :object
+ setQuery(string)                        :object
+ insert(string, arrays)                :void
+ update(string, array)                    :void
+ delete(string)                        :void
+ values(string)                        :object
+ where(string, mixed)                    :object
+ order(string, string)                    :object
+ group(string)                            :object
+ tableJoin(string, string, string)        :object
+ limit(integer, integer)                :object
+ getValues()                            :string
+ getWhere()                            :string
+ getOrder()                            :string
+ getGroup()                            :string
+ getTableJoin()                        :string
+ getLimit()                            :string
+ get(string, bool)                        :array
- _reset()                                :void
+ haveRows(string)                        :bool
+ numRows()                                :integer
+ insertId()                            :mixed
+ rowsAfected()                            :mixed
+ freeResult()                            :mixed
+ disconnect()                            :void
+ myQuery()                                :string
+ execute()                                :bool
+ fetchQuery(bool)                        :array
+ scape(mixed)                            :mixed
*/
?>