PAGE OUTDATED

The current page is out-of-date. Please refer to the new documentation to know about querying database:
http://glpi-developer-documentation.readthedocs.io/en/master/devapi/querydb.html

Database Usage

CommonDBTM object

Need properties

Need methods

DBmysql object

request (available in 0.72 only)

This simple method provides an simple iterator on database request.

It generate the SQL statement and call the other methods for you (query, fetch_assoc, free_result).

Simplest example : browsing a full table

   foreach ($DB->request("glpi_dropdown_state") as $data) {
   }

Compatibility mode example :

   $sql = "SELECT what you want to be retrieve...";
   foreach ($DB->request($sql) as $data) {
   }

The second optional option ($crit) provides criterias for rows / columns to be retrieve

- field or list of fields (default: all)
   array('FIELDS'=>'name')
   array('FIELDS'=>array('ID','name'))
   array('FIELDS'=>array('glpi_table_1'=>'ID', 'glpi_table_2'=>array('name',foo)))
- sort order of the result (ORDER BY clause)
   array('ORDER'=>'name')
   array('ORDER'=>array('lastname','firstname')
- the row index to be retrieved (LIMIT clause)
   array('LIMIT'=>20)
   array('START'=>10, 'LIMIT'=>20)
- the criterias on the row (WHERE clause)
   # Simple criterias
   array('ID'=>1)
   array('ID'=>NULL)
   array('ID'=>array(1,2,3))
   array('deleted'=>0, 'is_template'=1)

   # Foreign Keys
   'FKEY'=>array('glpi_table_1'=>'field_1', 'glpi_table_2'=>'field_2')

   # Boolean operator
   'OR' => <criteria>
   'AND' => <criteria>
   'NOT' => <criteria>

The big example :

foreach($DB->request(array("glpi_computers","glpi_dropdown_domain"),
    array ('FIELDS'=>array("glpi_computers"=>array("ID", "name"), "glpi_dropdown_domain"=>"name AS domain"),
        'START'=>5, 'LIMIT'=>10,
        'FKEY'=>array('glpi_computers'=>'domain','glpi_dropdown_domain'=>'ID'),
        'FK_entities'=>1, 'deleted'=>0, 'is_template'=>0, 
        'OR'=>array("state"=>array(3,4), 
            'NOT'=>array("network"=>0)))
       ) as $data) {
    echo "+".$data[[name"]""$data["domain]]."\n";
}

Will generate the following SQL query :

SELECT glpi_computers.ID,glpi_computers.name,glpi_dropdown_domain.name AS domain 
  FROM glpi_computers, glpi_dropdown_domain 
  WHERE glpi_computers.domain=glpi_dropdown_domain.ID AND FK_entities=1 AND deleted=0 AND is_template=0 AND (state IN ('3','4') OR  NOT (network=0)) 
  ORDER BY name 
  LIMIT 10 OFFSET 5