ADOdb

Database Abstraction Layer for PHP

User Tools

Site Tools


v5:userguide:learn_abstraction:using_execute

This is an old revision of the document!


Using The Execute Method

The command execute() provides complete control over the reading and writing of the data in the database.

Reading Data

When used to read the data, the executed command returns a Query Result, which can be accessed an used to read the records individually.

$sql = "select * from employees";
$result = $db->execute($sql);
/*
* $result is our query result
*/

Once the result set is available, there are numerous ways to retrieve the records in the result. One way to retrieve the data is to use the fetchRow() method, which reads the current record into an array, and advances the recordset pointer. This means that the loop will end once end-of-file has been reached.

$sql = "select * from employees";
$result = $db->execute($sql);
while ($r = $result->fetchRow())
{
     print_r($r);
     /*
     * prints:
     * array('emp_no' => 1000,
              'emp_name' => 'Joe Smith',
              'hire_date' => '2014-01-12'
              )
     * etc, until then end of file 
     */
} 

Once the end of file has been reached, a flag, $result→EOF is set, and can be directly queried.

Other ways of reading the recordset are:

CommandDescription
fetchInto() Fetches a recordset into an array
fetchObj() Returns the current row as an object for convenience

If the DBMS supports the functionality, the recordset can be paged and scrolled with the Move methods

CommandDescription
move() Move to the nth record of a recordset
moveNext() Moves the cursor to the next record of the recordset from the current position
moveFirst() Moves the cursor to the first record of the recordset
moveLast() Moves to the last record of a recordset

Writing Data

When used to INSERT/UPDATE/DELETE data, a simple boolean is returned that indicates the success or failure of the operation. In addition, some databases will return a value which indicates the number of records changed by the operation. This value can be retrieved with the affected_rows() method.

$sql = "UPDATE employees
           SET emp_name='Zaphod Beeblebrox'
         WHERE emp_no=10000";
 
$ok = $db->execute($sql);
 
print $db->affected_rows();
 
/*
* Returns: 1
*/

See Also

selectLimit, which allows us to limit the number of rows shown.

v5/userguide/learn_abstraction/using_execute.1458001758.txt.gz · Last modified: 2017/04/21 11:39 (external edit)