CUBRID Tutorial for ADOdb Lite Library
The following tutorial will explain how to configure and use the basic functionalities of ADOdb Lite together with CUBRID database.
Required Software
To successfully replicate the following examples, it is necessary to install the following prerequisites:
- Apache Web Server
- PHP
- ADOdb Lite Library
Downloads
- For installation instructions of the above items, visit CUBRID Database with Apache and PHP on Linux.
- Detailed explanations about using PHP with CUBRID can be found in PHP Programming with CUBRID.
- Download ADOdblite for CUBRID.
- You can download the files with examples used in this tutorial below:
adodb-lite-tutorial.zip.
Examples
Scenario
Create a Database
First we must create a database that we will use throughout this tutorial. Open the CUBRID Manager, connect to CUBRID database server and create a new database named tutorial having username dba and password test.
Create a Table
Create two new tables ado_users and ado_users_logging with the structure described by the following SQL:
CREATE TABLE "ado_users"(
"user_id" integer AUTO_INCREMENT,
"username" character varying(64) NOT NULL UNIQUE,
"password" character varying(255) NOT NULL,
"name" character varying(64),
CONSTRAINT pk_ado_users_user_id PRIMARY KEY("user_id")
);
CREATE TABLE "ado_users_logging"(
"id" integer AUTO_INCREMENT,
"user_id" integer NOT NULL UNIQUE,
"login" datetime NOT NULL UNIQUE,
CONSTRAINT pk_ado_users_logging_id PRIMARY KEY("id"),
CONSTRAINT "fk_ul" FOREIGN KEY ("user_id") REFERENCES "ado_users"("user_id") ON DELETE RESTRICT ON UPDATE RESTRICT
);
Insert Records
Then add records to the tables:
INSERT INTO ado_users ( username, `password`, name ) VALUES ( 'john', 'moonlight', 'John Doe' ); INSERT INTO ado_users_logging(user_id, login) VALUES ( 1, '2010-10-10 14:34:44' );
Load Libraries
Create a tutorial.php file and include the library:
Establish a Database Connection
Connection to the database can be achieved in three ways. First method is to create a new ADONewConnection object and provide credentials when calling Connect:
$dbhost = 'localhost';
$dbuser = 'dba';
$dbpass = 'test';
$dbname = 'tutorial';
$db = ADONewConnection('cubrid');
$db->debug = true;
$db->Connect($dbhost, $dbuser, $dbpass, $dbname);
Second method allows you to specify all connection information into one string using DSN:
$dsn = 'cubrid://dba:test@localhost/tutorial?persist&clientflags=$flags#pear:extend'; $db = ADONewConnection($dsn);
Third method is provided for better compatibility with ADOdb application and allows you to set up the library via configuration file. By using adodb.config.php you can designate the modules when connecting to database, server etc. Read the file because contains the documentation needed to set up the library.
Execute a Query
Now that the configuration is complete we can start using the ADOdb Lite functionalities. Let's start with a simple query:
$rs = $db->Execute("SELECT * FROM ado_users");
print "";
print_r($rs->GetRows());
print "";
$rs->Close();
This will execute the query and output the associative array corresponding for the result set. It contains the two records we have in the table:
Array (
[0] => Array
(
[0] => 1
[user_id] => 1
[1] => ted
[username] => ted
[2] => secret
[password] => secret
[3] => Ted
[name] => Ted
)
[1] => Array
(
[0] => 2
[user_id] => 2
[1] => john
[username] => john
[2] => moonlight
[password] => moonlight
[3] => John Doe
[name] => John Doe
)
)
If you want to iterate through the each row you can use the following code:
echo '<table border="1"><tr><th>User ID</th><th>Username</th><th>Password</th><th>Name</th></tr>';
while (!$rs->EOF) {
echo '<tr>';
echo '<td>' . $rs->fields[0] . '</td>';
echo '<td>' . $rs->fields[1] . '</td>';
echo '<td>' . $rs->fields[2] . '</td>';
echo '<td>' . $rs->fields[3] . '</td>';
echo '</tr>';
$rs->MoveNext();
}
echo '</table>';
$rs->Close();
Another faster alternative method to retrieve the results can be achieved using
Insert Values
The following snippet will insert a new value into the ado_users table:
$sql = "INSERT INTO ado_users(username, `password`, name) VALUES (";
$sql .= $db->qstr('chris', get_magic_quotes_gpc()) . ", ";
$sql .= $db->qstr("secretpass", get_magic_quotes_gpc()) . ", ";
$sql .= $db->qstr('Chris', get_magic_quotes_gpc()) . ");";
echo $sql;
if ( $db->Execute($sql) === false ) {
print 'Error inserting: '.$db->ErrorMsg().'<br />';
} else {
echo 'Row successfully added';
}
Tip: We have used here qstr to correctly quote the string before sending it to the database.
UPDATE and DELETE
Similar to executing SELECTs, update and delete statements can be executed on database connection object.
// Delete values
$sql = "DELETE FROM ado_users WHERE username = 'chris'";
if ( $db->Execute($sql) !== false ) {
echo 'Row successfully deleted. Affected rows: ' . $db->Affected_Rows();
} else {
print 'Error deleting row: '.$db->ErrorMsg().'<br />';
}
// Update values
$sql = "UPDATE ado_users SET name = 'Ted Willis' WHERE username = 'ted'";
if ( $db->Execute($sql) !== false ) {
echo 'Row successfully updated. Affected rows: ' . $db->Affected_Rows();
} else {
print 'Error updating row: '.$db->ErrorMsg().'<br />';
}
Handling DATE Values
To show how DATETIME values are used let's use the table ado_users_logging that can record the time when user has login into an website for example. Let's suppose we want to extract all users Ids that logged in year 2010:
$sql = "SELECT DISTINCT(user_id) FROM ado_users_logging WHERE login > " . $db->DBTimeStamp('2010-01-01 00:00:00');
echo $sql;
$rs = $db->Execute($sql);
In our case it will return a single ID with user_id 1.
Getting Help
If you have any difficulties, you can post your questions to PHP forum for CUBRID at http://www.cubrid.org/forum.
