<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/">
    <channel>
        <title>CUBRID Tutorial for ADOdb Lite Library</title>
        <link>http://www.cubrid.org/?mid=cubrid_tutorial_for_adodb_lite_library</link>
        <description>CUBRID Tutorial for ADOdb Lite Library</description>
        <language>en</language>
        <pubDate>Tue, 12 Oct 2010 17:36:53 -0800</pubDate>
        <lastBuildDate>Mon, 23 Jan 2012 06:18:24 -0800</lastBuildDate>
        <generator>XpressEngine 1.4.4.1</generator>
                        										        <item>
            <title>CUBRID Tutorial ...</title>
            <dc:creator>CUBRID</dc:creator>
            <link>http://www.cubrid.org/cubrid_tutorial_for_adodb_lite_library</link>
            <guid isPermaLink="true">http://www.cubrid.org/cubrid_tutorial_for_adodb_lite_library</guid>
                                    <description><![CDATA[<h1>CUBRID Tutorial for ADOdb Lite Library</h1>
<div class="category"><a href="/about_adodblite" style="color:#CCC">⇐ADOdblite for CUBRID</a></div>

<p>The following tutorial will explain how to configure and use the basic functionalities of ADOdb Lite together with CUBRID database.</p>

<h3>Required Software</h3>

<p>To successfully replicate the following examples, it is necessary to install the following prerequisites:</p>

<ul>
<li>Apache Web Server</li>
<li>PHP</li>
<li>ADOdb Lite Library</li>
</ul>

<h2>Downloads</h2>

<ul>
<li>For installation instructions of the above items, visit <a href="/using_cubrid_with_apache_php_in_linux" target="_blank">CUBRID Database with Apache and PHP on Linux</a>.</li>
<li>Detailed explanations about using PHP with CUBRID can be found in <a href="/php_api_for_cubrid" target="_blank">PHP Programming with CUBRID</a>.</li>
<li>Download <a href="/about_adodblite" target="_blank">ADOdblite for CUBRID</a>.</li>
<li>You can download the files with examples used in this tutorial below:<br />
<a href="/files/docs/tutorials/adodblite/adodb-lite-tutorial.zip" target="_self">adodb-lite-tutorial.zip</a>.</li>
</ul>

<h2>Examples</h2>

<h3>Scenario</h3>

<h4>Create a Database</h4>

<p>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.</p>

<img src="/files/attach/images/tutorials/adodb_lite/cubrid_manager_create_database.jpg" alt="CUBRID Manager: CREATE DATABASE" title="CUBRID Manager: CREATE DATABASE" editor_component="image_link"/>

<h4>Create a Table</h4>

<p>Create two new tables <b>ado_users</b> and <b>ado_users_logging</b> with the structure described by the following SQL:</p>

<div>
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(/modules/editor/components/code_highlighter/code.png) no-repeat top right;">
CREATE TABLE "ado_users"(<br />
	"user_id" integer AUTO_INCREMENT,<br />
	"username" character varying(64) NOT NULL UNIQUE,<br />
	"password" character varying(255) NOT NULL,<br />
	"name" character varying(64),<br />
	CONSTRAINT pk_ado_users_user_id PRIMARY KEY("user_id")<br />
);<br />
<br />
CREATE TABLE "ado_users_logging"(<br />
	"id" integer AUTO_INCREMENT,<br />
	"user_id" integer NOT NULL UNIQUE,<br />
	"login" datetime NOT NULL UNIQUE,<br />
	CONSTRAINT pk_ado_users_logging_id PRIMARY KEY("id"),<br />
	CONSTRAINT  "fk_ul" FOREIGN KEY ("user_id") REFERENCES "ado_users"("user_id") ON DELETE RESTRICT ON UPDATE RESTRICT<br />
);<br />
</div>
</div>

<h4>Insert Records</h4>

<p>Then add records to the tables:</p>

<div>
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(/modules/editor/components/code_highlighter/code.png) no-repeat top right;">
INSERT INTO ado_users ( username, `password`, name )  VALUES ( 'john', 'moonlight', 'John Doe' );<br />
INSERT INTO ado_users_logging(user_id, login) VALUES ( 1, '2010-10-10 14:34:44' );<br />
</div>
</div>

<h3>Load Libraries</h3>

<p>Create a <b>tutorial.php</b> file and include the library:</p>

<div>
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(/modules/editor/components/code_highlighter/code.png) no-repeat top right;">
</div>
</div>

<h3>Establish a Database Connection</h3>

<p>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:</p>

<div>
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(/modules/editor/components/code_highlighter/code.png) no-repeat top right;">
$dbhost = 'localhost';<br />
$dbuser = 'dba';<br />
$dbpass = 'test';<br />
$dbname = 'tutorial';<br />
$db = ADONewConnection('cubrid');<br />
$db-&gt;debug = true;<br />
$db-&gt;Connect($dbhost, $dbuser, $dbpass, $dbname);<br />
</div>
</div>

<p>Second method allows you to specify all connection information into one string using DSN:</p>

<div>
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(/modules/editor/components/code_highlighter/code.png) no-repeat top right;">
$dsn = 'cubrid://dba:test@localhost/tutorial?persist&amp;clientflags=$flags#pear:extend';
$db = ADONewConnection($dsn);<br />
</div>
</div>

<p>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.</p>

<h3>Execute a Query</h3>

<p>Now that the configuration is complete we can start using the ADOdb Lite functionalities. Let's start with a simple query:</p>

<div>
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(/modules/editor/components/code_highlighter/code.png) no-repeat top right;">
$rs = $db-&gt;Execute("SELECT * FROM ado_users");<br />
print "<pre>";<br />
print_r($rs-&gt;GetRows());<br />
print "</pre>";<br />
$rs-&gt;Close();<br />
</div>
</div>

<p>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:</p>

<div>
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(/modules/editor/components/code_highlighter/code.png) no-repeat top right;">
Array (<br />
    [0] =&gt; Array<br />
	(<br />
		[0] =&gt; 1<br />
		[user_id] =&gt; 1<br />
		[1] =&gt; ted<br />
		[username] =&gt; ted<br />
		[2] =&gt; secret<br />
		[password] =&gt; secret<br />
		[3] =&gt; Ted<br />
		[name] =&gt; Ted<br />
	)<br />
<br />
	[1] =&gt; Array<br />
	(<br />
		[0] =&gt; 2<br />
		[user_id] =&gt; 2<br />
		[1] =&gt; john<br />
		[username] =&gt; john<br />
		[2] =&gt; moonlight<br />
		[password] =&gt; moonlight<br />
		[3] =&gt; John Doe<br />
		[name] =&gt; John Doe<br />
	)<br />
)<br />
</div>
</div>

<p>If you want to iterate through the each row you can use the following code:</p>

<div>
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(/modules/editor/components/code_highlighter/code.png) no-repeat top right;">
echo '&lt;table border="1"&gt;&lt;tr&gt;&lt;th&gt;User ID&lt;/th&gt;&lt;th&gt;Username&lt;/th&gt;&lt;th&gt;Password&lt;/th&gt;&lt;th&gt;Name&lt;/th&gt;&lt;/tr&gt;';<br />
<br />
while (!$rs-&gt;EOF) {<br />
	echo '&lt;tr&gt;';<br />
	echo '&lt;td&gt;' . $rs-&gt;fields[0] . '&lt;/td&gt;';<br />
	echo '&lt;td&gt;' . $rs-&gt;fields[1] . '&lt;/td&gt;';<br />
	echo '&lt;td&gt;' . $rs-&gt;fields[2] . '&lt;/td&gt;';<br />
	echo '&lt;td&gt;' . $rs-&gt;fields[3] . '&lt;/td&gt;';<br />
	echo '&lt;/tr&gt;';<br />
	$rs-&gt;MoveNext();<br />
}<br />
<br />
echo '&lt;/table&gt;';<br />
$rs-&gt;Close();<br />
</div>
</div>

<p>Another faster alternative method to retrieve the results can be achieved using</p>

<h3>Insert Values</h3>

<p>The following snippet will insert a new value into the ado_users table:</p>

<div>
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(/modules/editor/components/code_highlighter/code.png) no-repeat top right;">
$sql = "INSERT INTO ado_users(username, `password`, name) VALUES (";<br />
$sql .= $db-&gt;qstr('chris', get_magic_quotes_gpc()) . ", ";<br />
$sql .= $db-&gt;qstr("secretpass", get_magic_quotes_gpc()) . ", ";<br />
$sql .= $db-&gt;qstr('Chris', get_magic_quotes_gpc()) . ");";<br />
<br />
echo $sql;<br />
<br />
if ( $db-&gt;Execute($sql) === false ) {<br />
	print 'Error inserting: '.$db-&gt;ErrorMsg().'&lt;br /&gt;';<br />
} else {<br />
	echo 'Row successfully added';<br />
}<br />
</div>
</div>

<p>Tip: We have used here qstr to correctly quote the string before sending it to the database.</p>

<h3>UPDATE and DELETE</h3>

<p>Similar to executing SELECTs, update and delete statements can be executed on database connection object.</p>

<div>
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(/modules/editor/components/code_highlighter/code.png) no-repeat top right;">
// Delete values<br />
$sql = "DELETE FROM ado_users WHERE username = 'chris'";<br />
<br />
if ( $db-&gt;Execute($sql) !== false ) {<br />
	echo 'Row successfully deleted. Affected rows: ' . $db-&gt;Affected_Rows();<br />
} else {<br />
	print 'Error deleting row: '.$db-&gt;ErrorMsg().'&lt;br /&gt;';<br />
}<br />
<br />
// Update values<br />
$sql = "UPDATE ado_users SET name = 'Ted Willis' WHERE username = 'ted'";<br />
<br />
if ( $db-&gt;Execute($sql) !== false ) {<br />
	echo 'Row successfully updated. Affected rows: ' . $db-&gt;Affected_Rows();<br />
} else {<br />
	print 'Error updating row: '.$db-&gt;ErrorMsg().'&lt;br /&gt;';<br />
}<br />
</div>
</div>

<h3>Handling DATE Values</h3>

<p>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:</p>

<div>
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border: #666666 1px dotted; border-left: #22aaee 5px solid; padding: 5px; background: #FAFAFA url(/modules/editor/components/code_highlighter/code.png) no-repeat top right;">
$sql = "SELECT DISTINCT(user_id) FROM ado_users_logging WHERE login &gt; " . $db-&gt;DBTimeStamp('2010-01-01 00:00:00');<br />
echo $sql;<br />
$rs = $db-&gt;Execute($sql);<br />
</div>
</div>

<p>In our case it will return a single ID with user_id 1.</p>

<h2>Getting Help</h2>

<p>If you have any difficulties, you can post your questions to PHP forum for CUBRID at <a href="/?mid=forum&amp;category=195537&amp;act=dispForumContent" target="_blank">http://www.cubrid.org/forum</a>.</p>]]></description>
                        <pubDate>Tue, 12 Oct 2010 18:37:07 -0800</pubDate>
                                </item>
            </channel>
</rss>
