db2_exec Using PHP Versi 5 [General]

Posted: Saturday,January 17, 2009 in DB2
Tags: , ,

Ini merupakan kode untuk koneksi ke database db2 dari php secara umum. Contoh kode nya menggunakan function db2_exec tanpa binding parameter.

//Asumsi modul db2 sudah terload atau sudah di setting memalui php.ini

//Asumsi letak htdocs di komputer Anda sama dengan saya

1. Create Folder PHP di dalam C:\Program Files\xampp\htdocs

2. Di C:\Program Files\xampp\htdocs\PHP

Buat new file dari notepad lalu save sebagai belajarkoneksidb2.php

3. Pastekan Kode berikut :

<?php
session_start();

$database = ‘SAMPLE’;
$user = ‘Username’;//Ganti dengan Username db2 Anda
$password = ‘Password’;//Gandi dengan Password db2 Anda
$hostname = ‘localhost’;
$port = 50000;

$conn_string = “DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$database;” .
“HOSTNAME=$hostname;PORT=$port;PROTOCOL=TCPIP;UID=$user;PWD=$password;”;
$conn = db2_connect($conn_string, ”, ”);

$sql = “SELECT lastname FROM emp”;

$result = db2_exec($conn, $sql);

while ($row = db2_fetch_assoc($result)) {
print “{$row[‘LASTNAME’]}<br>”;
}

?>

4. Start/Restart Xampp Anda

5. Browse Mozilla atau IE anda lalu ketikkan :

http://localhost/PHP/belajarkoneksidb2.php

6. Hasilnya ialah :

HAAS
THOMPSON
KWAN
GEYER
STERN
PULASKI
HENDERSON
SPENSER
LUCCHESSI
O’CONNELL
QUINTANA
NICHOLLS
ADAMSON
PIANKA
YOSHIMURA
SCOUTTEN
WALKER
BROWN
JONES
LUTZ
JEFFERSON
MARINO
SMITH
JOHNSON
PEREZ
SCHNEIDER
PARKER
SMITH
SETRIGHT
MEHTA
LEE
GOUNOT
HEMMINGER
ORLANDO
NATZ
YAMAMOTO
JOHN
MONTEVERDE
SCHWARTZ
SPRINGER
WONG
ALONZO

Have A Nice Try!!!

Good Luck…

Comments
  1. denysutani says:

    Terima kasih banyak wi ^^
    Nice inpo

  2. masslamet says:

    Lom bisa konek

    Fatal error call undefine db2_connect()

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s