<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Examples</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="cubrid.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.cubrid.html">CUBRID Functions</a></div> <div class="up"><a href="book.cubrid.html">CUBRID</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="cubrid.examples" class="chapter"> <h1>Examples</h1> <p class="para">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.</p> <p class="para">The example below uses the demodb database as an examples. By default it is created during the installation. Make sure it has been created.</p> <div class="example" id="example-996"> <p><strong>Example #1 Example of Data Retrieval</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <html><br /> <head><br /> <meta http-equiv="content-type" content="text/html; charset=euc-kr"><br /> </head><br /> <body><br /> <center><br /> <table border=2><br /> <span style="color: #0000BB"><?php<br /> </span><span style="color: #FF8000">/**<br /> * Set server information for CUBRID connection. host_ip is the IP<br /> * address where the CUBRID Broker is installed (localhost in this<br /> * example), and host_port is the port number of the CUBRID Broker.<br /> * The port number is the default given during the installation.<br /> * For details, see "Administrator's Guide."<br /> */<br /> </span><span style="color: #0000BB">$host_ip </span><span style="color: #007700">= </span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$host_port </span><span style="color: #007700">= </span><span style="color: #0000BB">33000</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$db_name </span><span style="color: #007700">= </span><span style="color: #DD0000">"demodb"</span><span style="color: #007700">;<br /> </span><span style="color: #FF8000">/**<br /> * Connect to CUBRID Server. Do not make the actual connection, but<br /> * only retain the connection information. The reason for not making<br /> * the actual connection is to handle transaction more efficiently<br /> * in the 3-tier architecture. <br /> */<br /> </span><span style="color: #0000BB">$cubrid_con </span><span style="color: #007700">= @</span><span style="color: #0000BB">cubrid_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$host_ip</span><span style="color: #007700">, </span><span style="color: #0000BB">$host_port</span><span style="color: #007700">, </span><span style="color: #0000BB">$db_name</span><span style="color: #007700">);<br /> <br /> if (!</span><span style="color: #0000BB">$cubrid_con</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Database Connection Error"</span><span style="color: #007700">;<br /> exit;<br /> }<br /> </span><span style="color: #0000BB">?><br /></span> <span style="color: #0000BB"><?php<br /> $sql </span><span style="color: #007700">= </span><span style="color: #DD0000">"select sports, count(players) as players from event group by sports"</span><span style="color: #007700">;<br /> </span><span style="color: #FF8000">/**<br /> * Request the CUBRID Server for the results of the SQL statement.<br /> * Now make the actual connection to the CUBRID Server.<br /> */<br /> </span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">cubrid_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$cubrid_con</span><span style="color: #007700">, </span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /> <br /> if (</span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/**<br /> * Get the column names from the result set created by the SQL query.<br /> */<br /> </span><span style="color: #0000BB">$columns </span><span style="color: #007700">= </span><span style="color: #0000BB">cubrid_column_names</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /> </span><span style="color: #FF8000">/**<br /> * Get the number of columns in the result set created by the SQL query.<br /> */<br /> </span><span style="color: #0000BB">$num_fields </span><span style="color: #007700">= </span><span style="color: #0000BB">cubrid_num_cols</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">);<br /> </span><span style="color: #FF8000">/**<br /> * List the column names of the result set on the screen. <br /> */<br /> </span><span style="color: #007700">echo(</span><span style="color: #DD0000">"<tr>"</span><span style="color: #007700">);<br /> <br /> while (list(</span><span style="color: #0000BB">$key</span><span style="color: #007700">, </span><span style="color: #0000BB">$colname</span><span style="color: #007700">) = </span><span style="color: #0000BB">each</span><span style="color: #007700">(</span><span style="color: #0000BB">$columns</span><span style="color: #007700">)) {<br /> echo(</span><span style="color: #DD0000">"<td align=center></span><span style="color: #0000BB">$colname</span><span style="color: #DD0000"></td>"</span><span style="color: #007700">);<br /> }<br /> <br /> echo(</span><span style="color: #DD0000">"</tr>"</span><span style="color: #007700">);<br /> <br /> </span><span style="color: #FF8000">/**<br /> * Get the results from the result set.<br /> */<br /> </span><span style="color: #007700">while (</span><span style="color: #0000BB">$row </span><span style="color: #007700">= </span><span style="color: #0000BB">cubrid_fetch</span><span style="color: #007700">(</span><span style="color: #0000BB">$result</span><span style="color: #007700">)) {<br /> echo(</span><span style="color: #DD0000">"<tr>"</span><span style="color: #007700">);<br /> <br /> for (</span><span style="color: #0000BB">$i </span><span style="color: #007700">= </span><span style="color: #0000BB">0</span><span style="color: #007700">; </span><span style="color: #0000BB">$i </span><span style="color: #007700">< </span><span style="color: #0000BB">$num_fields</span><span style="color: #007700">; </span><span style="color: #0000BB">$i</span><span style="color: #007700">++) {<br /> echo(</span><span style="color: #DD0000">"<td align=center>"</span><span style="color: #007700">);<br /> echo(</span><span style="color: #0000BB">$row</span><span style="color: #007700">[</span><span style="color: #0000BB">$i</span><span style="color: #007700">]);<br /> echo(</span><span style="color: #DD0000">"</td>"</span><span style="color: #007700">);<br /> }<br /> <br /> echo(</span><span style="color: #DD0000">"</tr>"</span><span style="color: #007700">);<br /> }<br /> }<br /> </span><span style="color: #FF8000">/**<br /> * The PHP module in the CUBRID runs in a 3-tier architecture. Even when<br /> * calling SELECT for transaction processing, it is processed as a part<br /> * of the transaction. Therefore, the transaction needs to be rolled back<br /> * by calling commit or rollback even though SELECT was called for smooth<br /> * performance.<br /> */<br /> </span><span style="color: #0000BB">cubrid_commit</span><span style="color: #007700">(</span><span style="color: #0000BB">$cubrid_con</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">cubrid_disconnect</span><span style="color: #007700">(</span><span style="color: #0000BB">$cubrid_con</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">?><br /></span> </body><br /> </html></span> </code></div> </div> </div> <div class="example" id="example-997"> <p><strong>Example #2 Example of Data Insertion</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <html><br /> <head><br /> <meta http-equiv="content-type" content="text/html; charset=euc- kr"><br /> </head><br /> <body><br /> <center><br /> <table border=2><br /> <span style="color: #0000BB"><?php<br /> </span><span style="color: #FF8000">/**<br /> * host_ip is the IP address where the CUBRID Broker is installed<br /> * host_port is the port number of the CUBRID Broker<br /> * db_name is the name of CUBRID Database<br /> */<br /> </span><span style="color: #0000BB">$host_ip </span><span style="color: #007700">= </span><span style="color: #DD0000">"localhost"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$host_port </span><span style="color: #007700">= </span><span style="color: #0000BB">33000</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$db_name </span><span style="color: #007700">= </span><span style="color: #DD0000">"demodb"</span><span style="color: #007700">;<br /> </span><span style="color: #0000BB">$cubrid_con </span><span style="color: #007700">= @</span><span style="color: #0000BB">cubrid_connect</span><span style="color: #007700">(</span><span style="color: #0000BB">$host_ip</span><span style="color: #007700">, </span><span style="color: #0000BB">$host_port</span><span style="color: #007700">, </span><span style="color: #0000BB">$db_name</span><span style="color: #007700">);<br /> <br /> if (!</span><span style="color: #0000BB">$cubrid_con</span><span style="color: #007700">) {<br /> echo </span><span style="color: #DD0000">"Database Connection Error"</span><span style="color: #007700">;<br /> exit;<br /> }<br /> </span><span style="color: #0000BB">?><br /></span> <span style="color: #0000BB"><?php<br /> $sql </span><span style="color: #007700">= </span><span style="color: #DD0000">"insert into olympic (host_year,host_nation,host_city,"<br /> </span><span style="color: #007700">. </span><span style="color: #DD0000">"opening_date,closing_date) values (2008, 'China', 'Beijing',"<br /> </span><span style="color: #007700">. </span><span style="color: #DD0000">"to_date('08-08-2008','mm-dd- yyyy'),to_date('08-24-2008','mm-dd-yyyy')) ;"<br /> </span><span style="color: #0000BB">$result </span><span style="color: #007700">= </span><span style="color: #0000BB">cubrid_execute</span><span style="color: #007700">(</span><span style="color: #0000BB">$cubrid_con</span><span style="color: #007700">, </span><span style="color: #0000BB">$sql</span><span style="color: #007700">);<br /> if (</span><span style="color: #0000BB">$result</span><span style="color: #007700">) {<br /> </span><span style="color: #FF8000">/**<br /> * Handled successfully, so commit.<br /> */<br /> </span><span style="color: #0000BB">cubrid_commit</span><span style="color: #007700">(</span><span style="color: #0000BB">$cubrid_con</span><span style="color: #007700">);<br /> echo(</span><span style="color: #DD0000">"Inserted successfully "</span><span style="color: #007700">);<br /> } else {<br /> </span><span style="color: #FF8000">/**<br /> * Error occurred, so the error message is output and rollback is called.<br /> */<br /> </span><span style="color: #007700">echo(</span><span style="color: #0000BB">cubrid_error_msg</span><span style="color: #007700">());<br /> </span><span style="color: #0000BB">cubrid_rollback</span><span style="color: #007700">(</span><span style="color: #0000BB">$cubrid_con</span><span style="color: #007700">);<br /> }<br /> </span><span style="color: #0000BB">cubrid_disconnect</span><span style="color: #007700">(</span><span style="color: #0000BB">$cubrid_con</span><span style="color: #007700">);<br /> </span><span style="color: #0000BB">?><br /></span> </body><br /> </html></span> </code></div> </div> </div> </div> <hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="cubrid.constants.html">Predefined Constants</a></div> <div class="next" style="text-align: right; float: right;"><a href="ref.cubrid.html">CUBRID Functions</a></div> <div class="up"><a href="book.cubrid.html">CUBRID</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>