Examples

The following is a simple example that establishes a connection between PHP and CUBRID. This section will cover the most basic and notable features. The following code required to connect to CUBRID database, which means CUBRID Server and CUBRID Broker have to be running.

The example below uses the demodb database as an examples. By default it is created during the installation. Make sure it has been created.

Example #1 Example of Data Retrieval

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=euc-kr">
</head>
<body>
<center>
<table border=2>
<?php

$host_ip = "localhost";
$host_port = 33000;
$db_name = "demodb";

$cubrid_con = @cubrid_connect($host_ip, $host_port, $db_name);

if (!
$cubrid_con) {
echo
"Database Connection Error";
exit;
}
?>
<?php
$sql
= "select sports, count(players) as players from event group by sports";

$result = cubrid_execute($cubrid_con, $sql);

if (
$result) {

$columns = cubrid_column_names($result);

$num_fields = cubrid_num_cols($result);

echo "<tr>";

while (list(
$key, $colname) = each($columns)) {
echo
"<td align=center>$colname</td>";
}

echo
"</tr>";


while ($row = cubrid_fetch($result)) {
echo
"<tr>";

for (
$i = 0; $i < $num_fields; $i++) {
echo
"<td align=center>";
echo
$row[$i];
echo
"</td>";
}

echo
"</tr>";
}
}

cubrid_commit($cubrid_con);
cubrid_disconnect($cubrid_con);
?>
</body>
</html>

Example #2 Example of Data Insertion

<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=euc- kr">
</head>
<body>
<center>
<table border=2>
<?php

$host_ip = "localhost";
$host_port = 33000;
$db_name = "demodb";
$cubrid_con = @cubrid_connect($host_ip, $host_port, $db_name);

if (!
$cubrid_con) {
echo
"Database Connection Error";
exit;
}
?>
<?php
$sql
= "insert into olympic (host_year,host_nation,host_city,"
. "opening_date,closing_date) values (2008, 'China', 'Beijing',"
. "to_date('08-08-2008','mm-dd- yyyy'),to_date('08-24-2008','mm-dd-yyyy')) ;";
$result = cubrid_execute($cubrid_con, $sql);
if (
$result) {

cubrid_commit($cubrid_con);
echo
"Inserted successfully ";
} else {

echo cubrid_error_msg();
cubrid_rollback($cubrid_con);
}
cubrid_disconnect($cubrid_con);
?>
</body>
</html>
To Top