<?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 MySQL Compatibility Library</title>
        <link>http://www.cubrid.org/?mid=mysql_php_compatibility_library</link>
        <description>CUBRID MySQL Compatibility Library</description>
        <language>en</language>
        <pubDate>Wed, 09 Jun 2010 17:56:47 -0800</pubDate>
        <lastBuildDate>Thu, 12 Apr 2012 01:26:17 -0800</lastBuildDate>
        <generator>XpressEngine 1.4.4.1</generator>
                        										        <item>
            <title>CUBRID MySQL PH...</title>
            <dc:creator>CUBRID</dc:creator>
            <link>http://www.cubrid.org/mysql_php_compatibility_library</link>
            <guid isPermaLink="true">http://www.cubrid.org/mysql_php_compatibility_library</guid>
                                    <description><![CDATA[<h1>CUBRID MySQL PHP Compatibility library</h1>

<div class="contents-table"></div>

<blockquote class="q4"><p><span style="color: rgb(255, 0, 0); "><b>IMPORTANT!!!</b></span>&nbsp;This tutorial was create for CUBRID before version 8.3.x. Since then <a href="/wiki_apis/entry/cubrid-php-driver" target="_self">CUBRID PHP API</a> has been significantly improved which now provides high compatibility with MySQL API. Therefore, please DO NOT refer to this tutorial unless for knowledge only.</p></blockquote><p>The scope of this library is to make easier for developers to migrate PHP applications from MySQL to CUBRID.</p>

<p>The current CUBRID PHP Connector is different from the one created for MySQL:</p>

<ul>
	<li>The names of the functions might be different (mysql_query vs. cubrid_execute)</li>
	<li>The order of the arguments might be different (mysql_connect)</li>
	<li>Sometime there is no corresponding functionality between MySQL and CUBRID (mysql_insert_id, for example)</li>
</ul>

<p>For the complete list of PHP API functions comparison see&nbsp;</p><p><a href="/cubrid_mysql_php_function_comparison">http://www.cubrid.org/cubrid_mysql_php_function_comparison</a>.</p>

<p>This makes difficult for developers to port application from MySQL to CUBRID, as many times there is no direct mapping. Therefore, we have developed this CUBRID MySQL PHP Compatibility Library, which acts as a intermediate layer between CUBRID and MySQL, and simplifies the migration process.</p>

<p>The idea behind the development of this library was to make conversion from MySQL to CUBRID as simple as just adding a prefix (“cubrid_”) to the existing PHP MySQL function names.</p>

<p>For example, instead of converting:</p>
<div class="code">
<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px;">
<p>mysql_query ($sql, $conn)</p>
</div>
</div>
<p>to </p>
<div class="code">
<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px;">
<p>cubrid_execute($conn, $sql)</p>
</div>
</div>
<p>the developer will just write:</p>
<div class="code">
<div editor_component="code_highlighter" code_type="Sql" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px;">
<p>cubrid_mysql_query ($sql, $conn)</p>
</div>
</div>

<h2 id="requirements">Requirements</h2>

<p>Since this compatibility library is written in PHP, you will need to install the <a href="/php_api_for_cubrid">CUBRID PHP Connector</a> along with the <a href="/downloads">CUBRID Database</a> itself.</p>

<h2 id="downloads">Downloads</h2>

<p>Download the CUBRID MySQL PHP Compatibility Library below.</p>

<table class="blackcap">
<thead>
<tr>
	<th>CUBRID MySQL PHP Compatibility Library</th>
	<th width="70px">Size</th>
</tr>
</thead>
<tbody>
<tr>
	<td><a href="/files/mysql/mysql-compat-library.zip" target="_blank">mysql-compat-library.zip</a></td>
	<td>43.8 KB</td>
</tr>
</tbody>
</table>

<h2 id="functions">Functions Mapping</h2>

<p>The below table shows how you can replace the MySQL PHP functions with the Compatibility Library PHP functions.</p>

<table class="blackcap rowbg">
<thead>
<tr>
<th width="40%">MySQL functions</th>
<th>CUBRID MySQL Compatibility Library functions</th>
</tr>
</thead>
<tbody>
<tr>
<td>mysql_connect($server, $username, $password, $database)</td>
<td>cubrid_mysql_connect($server, $username, $password, $database)</td>
</tr>
<tr>
<td>mysql_pconnect($server, $username, $password, $database)</td>
<td>cubrid_mysql_pconnect($server, $username, $password, $database)<br /><b>Note:</b> Internally, it is mapped to <b>cubrid_mysql_connect</b>.</td>
</tr>
<tr>
<td>mysql_close($link_identifier)</td>
<td>cubrid_mysql_close($link_identifier)</td>
</tr>
<tr>
<td>mysql_errno($link_identifier = '')</td>
<td>cubrid_mysql_errno($link_identifier = '')</td>
</tr>
<tr>
<td>mysql_query($query, $link_identifier)</td>
<td>cubrid_mysql_query_native($query, $link_identifier)<br /><b>Note:</b> Use this function if you don’t need to retrieve the last insert id after using this function.</td>
</tr>
<tr>
<td></td>
<td>cubrid_mysql_query($query, $link_identifier, $last_insert_id_column = '')<br /><b>Note:</b> Use this function if you need to retrieve the last insert id after using this function.</td>
</tr>
<tr>
<td>last_insert_id($last_insert_id_column, $link_identifier</td>
<td>cubrid_last_insert_id($last_insert_id_column, $link_identifier, $cubrid_var = "xyz")</td>
</tr>
<tr>
<td>mysql_unbuffered_query($query, $link_identifier)</td>
<td>cubrid_mysql_unbuffered_query($query, $link_identifier)</td>
</tr>
<tr>
<td>mysql_affected_rows($link_identifier)</td>
<td>cubrid_mysql_affected_rows($link_identifier)</td>
</tr>
<tr>
<td>mysql_free_result($link_identifier)</td>
<td>cubrid_mysql_free_result($link_identifier)</td>
</tr>
<tr>
<td>mysql_fetch_row($result)</td>
<td>cubrid_mysql_fetch_row($result)</td>
</tr>
<tr>
<td>mysql_fetch_array($result, $result_type = 0)</td>
<td>cubrid_mysql_fetch_array($result, $result_type = 0)</td>
</tr>
<tr>
<td>mysql_fetch_object($result)</td>
<td>cubrid_mysql_fetch_object($result)</td>
</tr>
<tr>
<td>mysql_move_cursor($result, $offset)</td>
<td>cubrid_mysql_move_cursor($result, $offset)</td>
</tr>
<tr>
<td>mysql_fetch_field($result)</td>
<td>cubrid_mysql_fetch_field($result)</td>
</tr>
<tr>
<td>mysql_insert_id($link_identifier = -1)</td>
<td>cubrid_mysql_insert_id($link_identifier = -1)</td>
</tr>
<tr>
<td>mysql_num_rows($resultSet)</td>
<td>cubrid_mysql_num_rows($resultSet)</td>
</tr>
<tr>
<td>mysql_num_cols($resultSet)</td>
<td>cubrid_mysql_num_cols($resultSet)</td>
</tr>
<tr>
<td>mysql_data_seek($result, $row_number)</td>
<td>cubrid_mysql_data_seek($result, $row_number)</td>
</tr>
<tr>
<td>mysql_get_server_info($link_identifier)</td>
<td>cubrid_mysql_get_server_info($link_identifier)</td>
</tr>
<tr>
<td>mysql_commit($link_identifier)</td>
<td>cubrid_mysql_commit($link_identifier)</td>
</tr>
<tr>
<td>mysql_rollback($link_identifier)</td>
<td>cubrid_mysql_rollback($link_identifier)</td>
</tr>
<tr>
<td>mysql_real_escape_string($unescaped_string, $link_identifier = '')</td>
<td>cubrid_mysql_real_escape_string($unescaped_string, $link_identifier = '')</td>
</tr>
<tr>
<td>mysql_select_db($database_name, $link_identifier = '')</td>
<td>cubrid_mysql_select_db($database_name, $link_identifier = '')<br /><b>Note:</b> This function doesn’t actually do anything in CUBRID. The database has already been selected during <b>cubrid_connect</b>.</td>
</tr>
<tr>
<td></td>
<td>cubrid_SHOW_TABLES($link_identifier)<br /><b>Note:</b> This function was added to create an alternative to the MySQL corresponding SQL support.</td>
</tr>
<tr>
<td></td>
<td>cubrid_DESCRIBE($table, $link_identifier)<br /><b>Note:</b> This function was added to create an alternative to the MySQL corresponding SQL support.</td>
</tr>
<tr>
<td></td>
<td>cubrid_SHOW_INDEX_FROM($table, $link_identifier)</td>
</tr>
<tr>
<td></td>
<td>cubrid_ColumnDataType($identifier, $table, $column)<br /><b>Note:</b> This function was added to provide an easy way to identify the data types for a column in a CUBRID table.</td>
</tr>
<tr>
<td>mysql_fetch_lengths($result)</td>
<td>cubrid_mysql_fetch_lengths($result)</td>
</tr>
<tr>
<td>mysql_field_flags($result, $field_offset)</td>
<td>cubrid_mysql_field_flags($result, $field_offset)</td>
</tr>
<tr>
<td>mysql_field_len($result, $field_offset)</td>
<td>cubrid_mysql_field_len($result, $field_offset)</td>
</tr>
<tr>
<td>mysql_field_name($result, $field_offset)</td>
<td>cubrid_mysql_field_name($result, $field_offset)</td>
</tr>
<tr>
<td>mysql_field_seek($result, $field_offset)</td>
<td>cubrid_mysql_field_seek($result, $field_offset)</td>
</tr>
<tr>
<td>mysql_field_table($result, $field_offset)</td>
<td>cubrid_mysql_field_table($result, $field_offset)</td>
</tr>
<tr>
<td>mysql_field_type($result, $field_offset)</td>
<td>cubrid_mysql_field_type($result, $field_offset)</td>
</tr>
<tr>
<td>mysql_result($result, $row, $field = 0)</td>
<td>cubrid_mysql_result($result, $row, $field = 0)</td>
</tr>
<tr>
<td>mysql_get_client_info()</td>
<td>cubrid_mysql_get_client_info()</td>
</tr>
<tr>
<td>mysql_fetch_assoc($result)</td>
<td>cubrid_mysql_fetch_assoc($result)</td>
</tr>
<tr>
<td>mysql_list_dbs($link_identifier)</td>
<td>cubrid_mysql_list_dbs($link_identifier)<br /><b>Note:</b> This function is provided only for compatibility, as it is not possible in CUBRID to switch/select databases without calling connect.</td>
</tr>
<tr>
<td></td>
<td>columnIsPK($dbl, $table, $column)<br /><b>Note:</b> This function was added to provide an easy way to identify the Primary Keys in a CUBRID table</td>
</tr>
<tr>
<td></td>
<td>cubrid_mysql_fix_keywords($word)<br /><b>Note:</b> This function was added to provide an easy way to quote specific CUBRID keywords.</td>
</tr>
</tbody>
</table>

<h2 id="specifics">CUBRID Specifics</h2>

<p>Due to the differences between MySQL architecture and the CUBRID architecture, there are some cases when there is no direct mapping between the PHP functions. Such cases are:</p>

<ul>
<li><b>LAST INSERT ID</b> - there is no corresponding CUBRID implementation. We choose to use 2 mysql_query implementations, one that provides last insert id information and one which doesn’t.
</li>
<li><b>SHOW TABLES</b> - there is no corresponding CUBRID implementation for this MySQL SQL statement. We choose to provide an equivalent function in the library.
</li>
<li><b>SHOW INDEXES</b> - there is no corresponding CUBRID implementation for this MySQL SQL statement. We choose to provide an equivalent function in the library.
</li>
<li><b>DESCRIBE TABLE</b> - there is no corresponding CUBRID implementation for this MySQL SQL statement. We choose to provide an equivalent function in the library.
</li>
</ul>

<p>The most important difference is probably in the implementation of the corresponding insert_id functionality in CUBRID. The rule is:</p>

<ul>
<li>If your mysql_query is not followed by a <b>mysql_insert_id</b> call, then instead of the <b>mysql_query</b> you should use the <b>cubrid_mysql_native_query</b> function.
</li>
<li>If your <b>mysql_query</b> is followed by a <b>mysql_insert_id</b> call, then instead of the <b>mysql_query</b> you should use the <b>cubrid_mysql_query</b> function and add, as an extra last argument to the function, the name of the column for which you need to retrieve the last insert id value. <b>Remember also</b>, if you need to deal with the last insert id functionality in your application, to call <b>$GLOBALS['calculate_last_insert_id'] = false;</b> when you initialize your application (one recommended way to do this is to make this call in the <b>cubrid_mysql_connect</b> function library implementation. The default is set to “false”, all you need to do is to change it to “true”).
</li>
</ul>

<h2 id="examples">Examples</h2>

<p>Here is a basic MySQL PHP program:</p>

<div class="code">
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px;">
&lt;?php<br />
	// we connect to localhost at port 3307<br />
	$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');<br />
	if (!$link) {<br />
	die('Could not connect: ' . mysql_error());<br />
	}<br />
	echo 'Connected successfully';<br />mysql_close($link);<br />
	?&gt; 
</div>
</div>

<p>And this is the converted CUBRID version:</p>

<div class="code">
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px;">
&lt;?php<br />// we connect to localhost at port 3307<br />$link = cubrid_mysql_connect('127.0.0.1:33000', 'dba', '', ‘demodb’);<br />if (!$link) {<br />    die('Could not connect: ' . cubrid_mysql_error());<br />}<br />echo 'Connected successfully';<br />cubrid_mysql_close($link);<br />?&gt; 
</div>
</div>

<p>Here is another simple example of MySQL PHP:</p>
<div class="code">
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px;">
&lt;?php<br />$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");<br />if (!$result) {<br />    echo 'Could not run query: ' . mysql_error();<br />    exit;<br />}<br />$row = mysql_fetch_row($result);<br /><br />echo $row[0]; // 42<br />echo $row[1]; // the email value<br />?&gt; 
</div>
</div>

<p>And this is the CUBRID converted code:</p>
<div class="code">
<div editor_component="code_highlighter" code_type="Php" file_path="" description="" first_line="1" collapse="false" nogutter="false" nocontrols="false" style="border-width: 1px 1px 1px 5px; border-style: dotted dotted dotted solid; border-color: rgb(102, 102, 102) rgb(102, 102, 102) rgb(102, 102, 102) rgb(34, 170, 238); padding: 5px;">
&lt;?php<br />$result = cubrid_mysql_native_query("SELECT id,email FROM people WHERE id = '42'");<br />if (!$result) {<br />    echo 'Could not run query: ' . mysql_error();<br />    exit;<br />}<br />$row = cubrid_mysql_fetch_row($result);<br /><br />echo $row[0]; // 42<br />echo $row[1]; // the email value<br />?&gt; 
</div>
</div>

<p>The conversion is quite easy and it significantly simplifies the conversion process from MySQL to CUBRID.</p>
<p class="important"><b>Note:</b> You can find code examples by looking at the implementation of the <b>fluxBB for CUBRID</b> (download will be available soon)</p>

<h2 id="tests">Sample Tests</h2>

<p>The library comes with a set of test programs. If you load, in a PHP-enabled web server, the “index.php” file, you will get a page from where you can run various tests developed for this library:</p>

<img alt="cubrid_php_mysql_compat_lib_tests.JPG" src="http://www.cubrid.org/files/attach/images/49/913/cubrid_php_mysql_compat_lib_tests.JPG" editor_component="image_link" height="171" width="641"/>

<p>Of course, we can use the code for the tests also as a guideline on how to use the library features.</p>

<p>The list of tests is improved continuously, by adding new tests, so we can get better code coverage.</p>

<h2 id="getting-help">Getting Help</h2>

<p>If you have questions regarding this CUBRID MySQL Compatibility Library, post your message at CUBRID Apps &amp; Tools.</p>

<p>We would appreciate very much your feedback on how we can improve this library.</p>

<h2 id="license">License and Disclaimer</h2>

<p>COPYRIGHT (C) 2010 CUBRID. ALL RIGHTS RESERVED.<br />
THIS PROGRAM IS FREE SOFTWARE; YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU GENERAL PUBLIC LICENSE AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION; EITHER VERSION 2 OF THE LICENSE, OR (AT YOUR OPTION) ANY LATER VERSION.<br />
THIS PROGRAM IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE
GNU GENERAL PUBLIC LICENSE FOR MORE DETAILS. YOU SHOULD HAVE RECEIVED A COPY OF THE GNU GENERAL PUBLIC LICENSE ALONG WITH THIS PROGRAM; IF NOT, WRITE TO THE FREE SOFTWARE FOUNDATION, INC., 51 FRANKLIN STREET, FIFTH FLOOR, BOSTON, MA 02110-1301 USA.</p>]]></description>
                        <pubDate>Fri, 11 Jun 2010 11:54:58 -0800</pubDate>
                        <category>php</category>
                        <category>api</category>
                        <category>mysql</category>
                                </item>
            </channel>
</rss>
