PHP (and yes it's for Python too..!) Adodb library has been one of the popular database abstract library since sometime now. We have been using it since sometime for simple operations when we are not using standard framework and probably just serving the plain web-services.

Here is a quick start guide to get started with Adodb5 library when using with PHP to get started with most basic functions for database interactions. For more details one can always browser around and read through detail documentation available at http://phplens.com/lens/adodb/docs-adodb.htm. This is just a quick start guide for all those who just want it little faster to get along with.

How to create adodb connection?

include_once(WEB_ROOT . 'base/adodb5/adodb.inc.php');
 
global $DB;
$DB	= NewADOConnection('mysqli');	
 
$DB->debug = false;
try
{
	$DB->Connect('localhost','root','password','my_db');
	$DB->fetchMode = ADODB_FETCH_ASSOC;
}
catch (Exception $e)
{
	print_r($e);
}

How to Query using Adodb?


Multiple Rows:

// Get all results returned by query
$Sql = "SELECT id, CONCAT(fname,' ',lname) as contact FROM contacts WHERE status='Active'";
$Result = $DB->GetAll($Sql);

Only one/first row from the result-set:

$Sql = "SELECT id, CONCAT(fname,' ',lname) as contact FROM contacts WHERE status='Active' ORDER BY date_of_birth ASC";
// Get single/first row from the result, we don't need to specify LIMIT 0,1 in query
$Result = $DB->GetRow($Sql);
// Above query will return youngest employee in the company..!

In above code we don't need to access array like $Result[0]['id'], we will be able to access directly like $Result['id']; :) which is a plus.

Only one column value returned directly:

$Sql = "SELECT COUNT(0) as ActiveContactCount FROM contacts WHERE status='Active'";
// Get count of active employee from emp_master table
$Result = $DB->GetOne($Sql);
// Above query will return count; and $Result will be a number directly instead of $rsResult like recordset..!

How to Insert record?

// Name of table
$Table = "<TableName>";
// All mandatory (NOT NULL) fields needs to be assigned value
$Record['<Field1>'] = "Some value";
$Record['<Field2>'] = "another value";
// Call AutoExecute function with following parameters and simply pass Action as "INSERT";
$DB->AutoExecute($Table, $Record, "INSERT");

How to get Last Inserted Records ID?

// Insert record
$DB->AutoExecute($Table, $Record, "INSERT");

OR

// Another way to insert record
$Sql = "INSERT INTO <TABLE> (<FIELD1>,<FIELD2>) VALUES ('<VAL1>','<VAL2>')";
$DB->Execute($strSql);
 
// and Get last insert id with following way
$DB->Insert_ID();

How to Update table record?

// Table
$Table = "<TableName>";
// Set field values
$Record['<Field1>'] = "Some value";
$Record['<Field2>'] = "another value";
// Must specify WHERE clause
$Where = "<FieldX> = '<VAL>'";
// Call AutoExecute function with following parameters and simply pass Action as "UPDATE";
$DB->AutoExecute($Table, $Record, "UPDATE", $Where);

OR

// Another way to update table record is (the manual one)
$Sql = "UPDATE TABLE <TABLE> (<FIELD1>,<FIELD2>) VALUES ('<VAL1>','<VAL2>') WHERE <FIELDX> = '<VAL>'";
// Call execute method.
$DB->Execute($strSql);

How to Execute any query directly?

// Write your SQL
$SQL = "<MY SQL: INSERT/UPDATE/ALTER/TRUNCATE/...";
// Just do it.!
$Result = $medDB->Execute($strSql);
// Play with your result-set depending on query type

How to find affected rows after update/insert/delete query?

// Write your SQL
// Your query execution statements
// Execute your SQL with Adodb db object and then...
$count = $DB->Affected_Rows();
 

Was these helpful? We would like to hear back.

Similar Posts: