Open Source RDBMS - Seamless, Scalable, Stable and Free

한국어 | Login |Register

Versions available for this page: CUBRID 8.2.1 |  CUBRID 8.3.0 |  CUBRID 8.3.1 |  CUBRID 8.4.0 |  CUBRID 8.4.1 |  CUBRID 8.4.3 |  CUBRID 9.0.0 | 

PHP Programming

General Features
Connecting to a Database

The first step of database applications is to use cubrid_connect() or cubrid_connect_with_url() function which provides database connection. Once 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 applications are  terminated. The cubrid_disconnect() function terminates the current transaction as well as the connection handle and all request handles created by the cubrid_connect() function.

Warning The database connection in thread-based programming must be used independently each other..

Transactions and Auto-Commit

CUBRID PHP supports 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. In auto-commit mode, any transactions being executed are committed regardless of whether it is set to ON or OFF.

The default value of auto-commit mode upon application startup is configured by the CCI_DEFAULT_AUTOCOMMIT (broker parameter). If the broker parameter value is not configured, the default value is set to ON. You can also use the cubrid_connect_with_url() function to set auto-commit mode as example shown below.

$con = cubrid_connect_with_url("cci:CUBRID:localhost:33000:demodb:dba::?autocommit=true");

If you set auto-commit mode to OFF in the cubrid_set_autocommit() function, you can handle transactions by specifying a proper function; to commit transactions, use the cubrid_commit() function and to roll back transactions, use the cubrid_rollback() function. If you use the cubrid_disconnect() function, transactions will be disconnected and jobs which have not been committed will be rolled back.

Processing Queries

Executing queries

Followings are the basic steps to execute queries.

  1. Creating a connection handle
  2. Creating a request handle for an SQL query request
  3. Fetching result
  4. Disconnecting the request handle

$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 arrays containing column types and the cubrid_column_types() functions is used to get arrays containing colunm 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);

}

Controlling a cursor

The cubrid_move_cursor() function is used to move a cursor to a specified position from one of three points: beginning of the query result, current cursor position, or 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"].” ”;

    }

}

Result array types

One of the following three types of arrays is used in the result of the cubrid_fetch() function. The array types can be determined when the cubrid_fetch() function is called. Of array types, the associative array uses string indexes and the numeric array uses number indexes. The last array includes both associative and numeric arrays.

  • Numeric array
  • while (list($id, $name) = cubrid_fetch($req, CUBRID_NUM)) {
  •     echo $id;
  •     echo $name;
  • }
  • Associative array
  • while ($row = cubrid_fetch($req, CUBRID_ASSOC)) {
  •     echo $row["id"];
  •     echo $row["name"];
  • }
Catalog Operations

The cubrid_schema() function is used to get database schema information such as classes, virtual classes, attributes, methods, triggers, and constraints. 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);

}

Error Handling

When an error occurs, most of PHP interfaces display error messages and return false or -1. The cubrid_error_msg(), cubrid_error_code() and cubrid_error_code_facility() functions are used to check error messages, error codes, and error facility codes.

The return value of the cubrid_error_code_facility() function is one of the followings (CUBRID_FACILITY_DBMS (DBMS error), CUBRID_FACILITY_CAS (CAS server error), CUBRID_FACILITY_CCI (CCI error), or CUBRID_FACILITY_CLIENT (PHP module error).

CUBRID Characteristics
Using OIDs

The OID value in the currently updated f record by using the cubrid_current_oid function if it is used together with query that can update the CUBRID_INCLUDE_OID option in the cubrid_execute() function.

$req = cubrid_execute($con, "select * from person where id = 1", CUBRID_INCLUDE_OID);

if ($req) {

    while ($row = cubrid_fetch($req)) {

        echo cubrid_current_oid($req);

        echo $row["id"];

        echo $row["name"];

    }

    cubrid_close_request($req);

}

Values in every attribute, specified attributes, or a single attribute of an instance can be obtained by using OIDs. If any attributes are not specified in the cubrid_get() function, values in every attribute are returned (a). If attributes is specified in the array data type, the array containing the specified attribute value is returned in the associative array (b). If a single attribute it is specified in the string type, a value of the attributed is returned (c).

$attrarray = cubrid_get ($con, $oid); // (a)

$attrarray = cubrid_get ($con, $oid, array("id", "name")); // (b)

$attrarray = cubrid_get ($con, $oid, "id"); // (c)

The attribute values of an instance can be updated by using OIDs. To update a single attribute value, specify attribute name and value in the string type (a). To update multiple attribute values, specify attribute names and values in the associative array (b).

$cubrid_put ($con, $oid, "id", 1); // (a)

$cubrid_put ($con, $oid, array("id"=>1, "name"=>"Tomas")); // (b)

Using Collections

You can use the collection data types through PHP array data types or functions that support array data types. The following example shows how to fetch query result by using the cubrid_fetch() function.

$row = cubrid_fetch ($req);

$col = $row["customer"];

while (list ($key, $cust) = each ($col)) {

   echo $cust;

}

You can get values of collection attributes. The example shows how to get values of collection attributes by using the cubrid_col_get() function.

$tels = cubrid_col_get ($con, $oid, "tels");

while (list ($key, $tel) = each ($tels)) {

   echo $tel." ";

}

You can directly update values of collection types by using cubrid_set_add() or cubrid_set_drop() function.

$tels = cubrid_col_get ($con, $oid, "tels");

while (list ($key, $tel) = each ($tels)) {

   $res = cubrid_set_drop ($con, $oid, "tel", $tel);

}

cubrid_commit ($con);

Note If a string longer than defined max length is inserted (INSERT) or updated (UPDATE), the string will be truncated.

Note To get the latest information about PHP driver, click http://www.cubrid.org/wiki_apis/entry/cubrid-php-driver.