Beispiele

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.

Beispiel #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
        
/**
         * Set server information for CUBRID connection. host_ip is the IP
         * address where the CUBRID Broker is installed (localhost in this
         * example), and host_port is the port number of the CUBRID Broker.
         * The port number is the default given during the installation.
         * For details, see "Administrator's Guide."
         */
        
$host_ip "localhost";
        
$host_port 33000;
        
$db_name "demodb";
        
/**
         * Connect to CUBRID Server. Do not make the actual connection, but
         * only retain the connection information. The reason for not making
         * the actual connection is to handle transaction more efficiently
         * in the 3-tier architecture. 
         */
        
$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";
        
/**
         * Request the CUBRID Server for the results of the SQL statement.
         * Now make the actual connection to the CUBRID Server.
         */
        
$result cubrid_execute($cubrid_con$sql);
 
        if (
$result) {
            
/**
             * Get the column names from the result set created by the SQL query.
             */
            
$columns cubrid_column_names($result);
            
/**
             * Get the number of columns in the result set created by the SQL query.
             */
            
$num_fields cubrid_num_cols($result);
            
/**
             * List the column names of the result set on the screen. 
             */
            
echo("<tr>");
 
            while (list(
$key$colname) = each($columns)) {
                echo(
"<td align=center>$colname</td>");
            }
 
            echo(
"</tr>");
 
            
/**
             * Get the results from the result set.
             */
            
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>");
            }
        }
        
/**
         * The PHP module in the CUBRID runs in a 3-tier architecture. Even when
         * calling SELECT for transaction processing, it is processed as a part
         * of the transaction. Therefore, the transaction needs to be rolled back
         * by calling commit or rollback even though SELECT was called for smooth
         * performance.
         */
        
cubrid_commit($cubrid_con);
        
cubrid_disconnect($cubrid_con);
    
?>
    </body>
    </html>

Beispiel #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 is the IP address where the CUBRID Broker is installed
         * host_port is the port number of the CUBRID Broker
         * db_name is the name of CUBRID Database
         */
        
$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) {
            
/**
             * Handled successfully, so commit.
             */
            
cubrid_commit($cubrid_con);
            echo(
"Inserted successfully ");
        } else {
            
/**
             * Error occurred, so the error message is output and rollback is called.
             */
            
echo(cubrid_error_msg());
            
cubrid_rollback($cubrid_con);
        }
        
cubrid_disconnect($cubrid_con);
    
?>
    </body>
    </html>

Hier Kannst Du einen Kommentar verfassen


Bitte gib mindestens 10 Zeichen ein.
Wird geladen... Bitte warte.
* Pflichtangabe
Es sind noch keine Kommentare vorhanden.

Grundlagen von Views in MySQL

Views in einer MySQL-Datenbank bieten die Möglichkeit, eine virtuelle Tabelle basierend auf dem Ergebnis einer SQL-Abfrage zu erstellen. ...

admin

Autor : admin
Kategorie: mySQL-Tutorials

Definition von Stored Procedures - eine Einführung

Stored Procedures sind vordefinierte SQL-Codeblöcke, die in einer Datenbank gespeichert sind und bei Bedarf aufgerufen werden können. ...

Bernie

Autor : ebiz-consult GmbH & Co. KG
Kategorie: mySQL-Tutorials

Wie kann man komplexe Abfragen mit SQL-Querys In MySQLi effektiv durchführen?

In diesem MySQL-Tutorial wird erklärt, wie komplexe SQL-Abfragen in MySQLi effizient durchgeführt werden können. Wir werden uns mit verschiedenen Aspekten der Datenbankabfrage beschäftigen und spezifische Methoden kennenlernen. ...

TheMax

Autor : TheMax
Kategorie: mySQL-Tutorials

Tutorial veröffentlichen

Tutorial veröffentlichen

Teile Dein Wissen mit anderen Entwicklern weltweit

Du bist Profi in deinem Bereich und möchtest dein Wissen teilen, dann melde dich jetzt an und teile es mit unserer PHP-Community

mehr erfahren

Tutorial veröffentlichen

[Wichtig] Anmeldung im Forum

Guess the hidden word using logic and deduction in the engaging game of Wordle. Wordle nyt (https://wordlenytimes.net/) ​

Geschrieben von lenytimes am 19.04.2024 19:21:45
Forum: Fragen/Vorschläge zum Forum
Professioneller Webentwickler & Webdesigner

Of course, here is the translation: Hello, Thank you for your interest in the long-term project. Your extensive skills and experience in web dev ...

Geschrieben von Athelstan am 15.04.2024 09:25:39
Forum: Jobgesuche
Wir stellen unsere SEO-Agentur vor

Hallo In der heutigen digitalen Welt war es für Unternehmen noch nie so einfach, ihre Reichweite weltweit zu vergrößern. Wenn Sie außerhalb I ...

Geschrieben von thomasmuller am 14.04.2024 07:18:33
Forum: User stellen sich vor
Spielplan für 4 Gruppen zu je 6 Teams auf 2 Feldern

Hallöchen zusammen, ich versuche derzeit unseren Excel-Spielplan in PHP zu überführen. Eigentlich bin ich auch shon fertig - wenn da nicht dies ...

Geschrieben von derH0st am 11.04.2024 15:58:37
Forum: PHP Developer Forum