文字

范例

下面是一个在PHP和CUBRID建立连接的简单的例子。本节将覆盖最基本和最显著的特性。下面的代码需要连接CUBRID数据库,这意味着 CUBRID服务和 CUBRID Broker已经运行。

下面用demodb数据库作为例子举例。默认在安装时就创建了。确认此数据库已经被创建。

Example #1 数据查询的例子

<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 数据插入的例子

<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>
上一篇: 下一篇: