Connecting to a database : The first step of a database application is to use the cubrid_connect() or cubrid_connect_with_url() function which provide a database connection. Once the cubrid_connect() or cubrid_connect_with_url() function is executed successfully, you can use any functions available in the database. It is very important to call the cubrid_disconnect() function before the application is terminated completely. The cubrid_disconnect() function terminates the current transaction as well as the connection handle and all request handles created by the cubrid_connect() function.
CUBRID PHP supports both transaction and auto-commit mode. Auto-commit mode means that every query that you run has its own implicit transaction. You can use the cubrid_get_autocommit() function to get the status of current connection auto-commit mode, and use the cubrid_set_autocommit() function to enable/disable auto-commit mode of current connection. When cubrid_set_autocommit() function is called, concurrent transactions are committed regardless of the auto-commit mode.
You can configure the default value of auto-commit mode by using CCI_DEFAULT_AUTOCOMMIT (broker parameter) upon startup of an application. If configuration on broker parameter is omitted, the default value is ON. You can also use cubrid_connect_with_url() function to set the autocommit-mode when you establish the database connection. For example:
$con = cubrid_connect_with_url("cci:CUBRID:localhost:33000:demodb:dba::?autocommit=true");
If you need a transaction, you must use the cubrid_set_autocommit() function to disable the auto-commit mode. The cubrid_commit() or cubrid_rollback() function is used to commit or roll back a transaction. The cubrid_disconnect() function terminates the transaction and rolls back uncommitted ones.
The following are basic steps of query execution.
$con = cubrid_connect("192.168.0.10", 33000, "demodb");
if($con) {
$req = cubrid_execute($con, "select * from code");
if($req) {
while ($row = cubrid_fetch($req)) {
echo $row["s_name"];
echo $row["f_name"];
}
cubrid_close_request($req);
}
cubrid_disconnect($con);
}
Column types and names of the query result
The cubrid_column_types() function is used to get an array containing column types, and the cubrid_column_names() function is used to get an array containing column names.
$req = cubrid_execute($con, "select host_year, host_city from olympic");
if($req) {
$col_types = cubrid_column_types($req);
$col_names = cubrid_column_names($req);
while (list($key, $col_type) = each($col_types)) {
echo $col_type;
}
while (list($key, $col_name) = each($col_names))
echo $col_name;
}
cubrid_close_request($req);
}
Adjusting the cursor
You can configure the position of the query result. The cubrid_move_cursor() function is used to move the cursor to a certain position from one of three points: the beginning of the query result, the current cursor position and the end of the query result.
$req = cubrid_execute($con, "select host_year, host_city from olympic order by host_year");
if($req) {
cubrid_move_cursor($req, 20, CUBRID_CURSOR_CURRENT)
while ($row = cubrid_fetch($req, CUBRID_ASSOC)) {
echo $row["host_year"].” “;
echo $row["host_city"].”\n”; }
}
Result array types
One of the following three types of arrays is used in the result of the cubrid_fetch() function. The type of the array can be determined when the cubrid_fetch() function is called. The associative array uses character string indexes. The numeric array uses numeric order indexes. The last array type includes both associative and numeric arrays.
Information about the database schema such as classes, virtual classes, attributes, functions, triggers and constraints can be obtained by calling the cubrid_schema() function. The return value of the cubrid_schema() function is a two-dimensional array.
$pk = cubrid_schema($con, CUBRID_SCH_PRIMARY_KEY, "game");
if ($pk) {
print_r($pk);
}
$fk = cubrid_schema($con, CUBRID_SCH_IMPORTED_KEYS, "game");
if ($fk) {
print_r($fk);
}
When an error occurs, most PHP interface functions display the error message and return false or -1. Each error message, error code or error facility code can be checked by using the cubrid_error_msg(), cubrid_error_code(), and cubrid_error_code_facility() functions.
The return value of the cubrid_error_code_facility() function is one of CUBRID_FACILITY_DBMS (DBMS error), CUBRID_FACILITY_CAS (CAS server error), CUBRID_FACILITY_CCI (CCI error) and CUBRID_FACILITY_CLIENT (PHP module error).