Open Source RDBMS - Seamless, Scalable, Stable and Free

English | Login |Register

CUBRID MySQL PHP Compatibility library

IMPORTANT!!! This tutorial was create for CUBRID before version 8.3.x. Since then CUBRID PHP API has been significantly improved which now provides high compatibility with MySQL API. Therefore, please DO NOT refer to this tutorial unless for knowledge only.

The scope of this library is to make easier for developers to migrate PHP applications from MySQL to CUBRID.

The current CUBRID PHP Connector is different from the one created for MySQL:

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

For the complete list of PHP API functions comparison see 

http://www.cubrid.org/cubrid_mysql_php_function_comparison.

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.

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.

For example, instead of converting:

mysql_query ($sql, $conn)

to

cubrid_execute($conn, $sql)

the developer will just write:

cubrid_mysql_query ($sql, $conn)

Requirements

Since this compatibility library is written in PHP, you will need to install the CUBRID PHP Connector along with the CUBRID Database itself.

Downloads

Download the CUBRID MySQL PHP Compatibility Library below.

CUBRID MySQL PHP Compatibility Library Size
mysql-compat-library.zip 43.8 KB

Functions Mapping

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

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

CUBRID Specifics

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:

  • LAST INSERT ID - 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.
  • SHOW TABLES - there is no corresponding CUBRID implementation for this MySQL SQL statement. We choose to provide an equivalent function in the library.
  • SHOW INDEXES - there is no corresponding CUBRID implementation for this MySQL SQL statement. We choose to provide an equivalent function in the library.
  • DESCRIBE TABLE - there is no corresponding CUBRID implementation for this MySQL SQL statement. We choose to provide an equivalent function in the library.

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

  • If your mysql_query is not followed by a mysql_insert_id call, then instead of the mysql_query you should use the cubrid_mysql_native_query function.
  • If your mysql_query is followed by a mysql_insert_id call, then instead of the mysql_query you should use the cubrid_mysql_query 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. Remember also, if you need to deal with the last insert id functionality in your application, to call $GLOBALS['calculate_last_insert_id'] = false; when you initialize your application (one recommended way to do this is to make this call in the cubrid_mysql_connect function library implementation. The default is set to “false”, all you need to do is to change it to “true”).

Examples

Here is a basic MySQL PHP program:

<?php
	// we connect to localhost at port 3307
	$link = mysql_connect('127.0.0.1:3307', 'mysql_user', 'mysql_password');
	if (!$link) {
	die('Could not connect: ' . mysql_error());
	}
	echo 'Connected successfully';
mysql_close($link);
	?> 

And this is the converted CUBRID version:

<?php
// we connect to localhost at port 3307
$link = cubrid_mysql_connect('127.0.0.1:33000', 'dba', '', ‘demodb’);
if (!$link) {
   die('Could not connect: ' . cubrid_mysql_error());
}
echo 'Connected successfully';
cubrid_mysql_close($link);
?> 

Here is another simple example of MySQL PHP:

<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
   echo 'Could not run query: ' . mysql_error();
   exit;
}
$row = mysql_fetch_row($result);

echo $row[0]; // 42
echo $row[1]; // the email value
?> 

And this is the CUBRID converted code:

<?php
$result = cubrid_mysql_native_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
   echo 'Could not run query: ' . mysql_error();
   exit;
}
$row = cubrid_mysql_fetch_row($result);

echo $row[0]; // 42
echo $row[1]; // the email value
?> 

The conversion is quite easy and it significantly simplifies the conversion process from MySQL to CUBRID.

Note: You can find code examples by looking at the implementation of the fluxBB for CUBRID (download will be available soon)

Sample Tests

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:

cubrid_php_mysql_compat_lib_tests.JPG

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

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

Getting Help

If you have questions regarding this CUBRID MySQL Compatibility Library, post your message at CUBRID Apps & Tools.

We would appreciate very much your feedback on how we can improve this library.

License and Disclaimer

COPYRIGHT (C) 2010 CUBRID. ALL RIGHTS RESERVED.
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.
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.

See also

CUBRID it! Winners

The 2nd CUBRID Contest has come to an end - it’s time to bring down the curtains and announce the happy winners!

CUBRID 8.4.0 Key Features

The new CUBRID 8.4.0 features many significant improvements which includes Performance Improvements, Developer Productivity Improvements and HA Reliab...

CUBRID vs. MySQL Benchmark Test Results for SNS Data and Workload

As we have recently rolled out the new 8.4.0 version of the CUBRID Database, one of our CUBRID users has approached us with a proposal t...

Increasing Database Performance by Query Tuning

It is common that applications use various kinds of SQL queries when communicating with the database server. If queries are not well structured, they wi...




You are either using a very old browser or a browser that is not supported.
In order to browse cubrid.org you need to have one of the following browsers:



Internet Explorer: Mozilla Firefox: Google Chrome: