New Sado Version 1.1 released! Easily add ORM to your applications using our Sado Library

Sado Connection Object Documentation

The Sado connection object controls all communication with the database. The connection object can be used to query the database and complete tasks like selects, inserts, updates and calling stored procedures.

To start using the connection object, include the bootstrap file and set an instance of the object: require_once 'lib/Sado/sado.bootstrap.php';

// set instance of object
$db = &SadoFactory::getInstance();

// here would be how to set instance of connection #2 (if it is setup)
// $db = &SadoFactory::getInstance(2);
Now we can start using the connection object. Here are the connection object methods and properties explained: // ping the server to test for a valid connection
if($db->ping())
{
      echo 'good connection';
}
else
{
      echo 'bad connection';
}

// get the instance ID for an instance
$instance_id = $db->getInstanceId();

// simple select
$db->select(' SELECT * FROM users; ');

// check if select returned rows
if($db->num_rows > 0)
{
      // loop through records
      foreach($db->getRecords as $user)
      {
            echo $user['fullname'];
      }
}
else
{
      echo 'no records';
}

// to protect against SQL injection use brackets to auto escape values
$db->select(" SELECT * FROM users WHERE user_id = {'$user_id'}; ");

// delete record - like saying delete from users where fullname = 'John Smith'
if($db->delete('users', array('fullname' => 'John Smith')))
{
      $deleted_rows = $db->affected_rows;
}

// insert record
$db->insert('users', array('fullname' => 'Mr. Foo', 'email' => 'email@domain.com', 'is_active' => 1));

// check if record inserted
if($db->affected_rows > 0)
{
      // get insert ID
      echo 'insert ID: ' . $db->getInsertId();
}

// update record - like saying update fullname to 'Mr. Anderson' where fullname = 'Shay Anderson'
$db->update('users', array('fullname' => 'Mr. Anderson'), array('fullname' => 'Shay Anderson'));

// show update records:
echo 'updated: ' . $db->affected_rows;

// call a stored procedure:
// first param is SP name, then all param passed to SP separated by commas
$db->call('sp_name', 5, 10);

// checking for DB error after method call:
if($db->is_error)
{
      // display error
      echo 'Error: ' . $db->error;
}

// get table primary key (string)
$key = $db->getModelKey('users');

// get table fields: (array)
$fields = $db->getModelTableFields('users');

// get tables in DB (array)
$db->getModelTables();

// select builder can be helpful for building query and selecting records:
$records = $db->selectBuilder()
            ->select('user_id, fullname')
            ->from('users')
            ->where('is_active', 1)
            ->order('user_id')
            ->limit(1)
            ->get();

// close DB connection
// this will be auto called so not necessary to call
$db->close();