Sei sulla pagina 1di 7

<?

php
class odbc_conn
{
var $server_name = "127.0.0.1";
var $db_host = "127.0.0.1";
var $db_user = "db2inst1";
var $db_pwd = "kosong";
var $db_name = "SAMPLE";
var $db_port ='50000';
var $maxlifetime=15000;
var $conn = null;
function odbc_conn()
{
odbc_conn::open_conn();
}
function open_conn()
{
$conn_string = "DRIVER={IBM DB2 ODBC DRIVER};DATABASE=$t
his->db_name;" .
"HOSTNAME=$this->db_host;PORT=$this->db_port;PRO
TOCOL=TCPIP;UID=$this->db_user;PWD=$this->db_pwd;";
$this->conn=db2_connect($conn_string, '', '');
if(($this->conn))
return true;
else
return false;
}
function close_conn()
{
return db2_close($this->conn);
}
function exec_query($sql)
{
if(!empty($sql)){
$query = db2_exec($this->conn, $sql, array('cursor' => DB2_SCROL
LABLE));
return $query;
} else{
$this->close_conn();
return false;
}
}
function fetch_data($query,$marker=0,$row_number=NULL,$arr_data=NULL)
{
$line = null;
if ($query != null) {
if (db2_num_rows($query) > 0) {
switch($marker){
case 0:
$row = db2_fetch
_array($query,$row_number);
if ($row) {
$columns
= db2_num_fields($query);
for($i=0
;$i<$columns;$i++){
$line[db2_field_name($query,$i)] = $row[$i];
// echo "<br>".db2_field_name($query,$i);

}
}
break;
case 1:
$line = db2_fetch_object
($query,$row_number);
break;
case 2:
if($query==NULL)
print "Parameter
$ res_id is needed";
else
$line =
db2_fetch_row($query,$row_number);
break;
case 3:
$line = db2_fetch_into($
query,$arr_data);
default:
return false;
}
}
}
return $line;
}
function fetch_array($query,$row_number=NULL)
{
$line = null;
if ($query != null) {
if (db2_num_rows($query) > 0) {
$row = db2_fetch_array($query,$r
ow_number);
if ($row) {
$columns = db2_num_field
s($query);
for($i=0;$i<$columns;$i+
+){
$line[db
2_field_name($query,$i)] = $row[$i];
// echo "<br>".d
b2_field_name($query,$i);
}
}
}
}
return $line;
}
function fetch_allarray($sql)
{
error_reporting(0);
$line = null;
$cols = array();
$result = db2_exec($this->conn, $sql);
$r=db2_num_rows($result);
$colcont=db2_num_fields($result);
if($r!=0)
{

for($i=0;$i<=$colcont;$i++)
{
$cols[]=db2_field_name($result,$i-1);
}
$row_flag=0;
while($rows = db2_fetch_assoc($result))
{
$line[] = $rows ;
}
}
return $line;
}
function fetch_row($query,$row_number=NULL)
{
$line = null;
if ($query != null) {
if (db2_num_rows($query) > 0) {
$line = db2_fetch_array($query,$
row_number);
}
}
return $line;
}
function num_rows($query)
{
$num = db2_num_rows($query);
if($num > 0)
return $num;
else
return false;
}
function num_fields($query)
{
$fields = db2_num_fields($query);
if($fields > 0)
return $fields;
else
return false;
}
function rollback()
{
if(is_resource($this->conn))
return db2_rollback($this->conn);
else
return false;
}
function commit()
{
if(is_resource($this->conn))
return db2_commit($this->conn);
else
return false;
}
function unset_conn()
{
return db2_close_all();
}

function db_info($fetch_type=1)
{
$info = db2_data_source($this->conn,$fetch_type);
foreach($info as $data)
{
$temp[] = $data;
}
return $temp;
}
function Prepare($query)
{
if(!empty($query) && is_resource($this->conn)){
$preExec = db2_do($this->conn,$query);
}
if(is_resource($preExec))
return
$preExec;
else
return false;
}
function go_next($query)
{
if(is_resource($query)){
$next = db2_next_result($query);
}
return $next;
}
function get_cursor($query)
{
if(!empty($query) && is_resource($query))
return db2_cursor($query);
else
return false;
}
function handle_columns($query,$length,$mode)
{
if(is_resource($query)&& is_int($length)){
switch($mode)
{
case 'len':
$_mode = db2_longreadlen($query,$length);
break;
case 'bin':
$_mode = db2_binmode($query,$length);
break;
default:
return false;
}
return $_mode;
}
}
function handle_fields($query,$value,$mode)
{
switch($mode){
case 'len':
$handle = db2_field_len($query,$value);
break;
case 'name':
$handle = db2_field_name($query,$value);
break;
case 'num':

$handle = db2_field_num($query,$value);
break;
case 'pres':
$handle = db2_field_precision($query,$value);
break;
case 'scale':
$handle = db2_field_scale($query,$value);
break;
case 'type':
$handle = db2_field_type($query,$value);
break;
default:
return false;
}
return $handle;
}
function Insert ($fields)
{
if(is_array($fields))
{
foreach($fields as $rows => $values){
$arrRows[] = "".$rows."";
$arrValues[]= "'".$values."'";
}
$strRows = implode(", ", $arrRows);
$strValues = implode(", ", $arrValues);
}
$query = "INSERT INTO ".$this->table."($strRows) VALUES ($strValues);";
$insert = $this->exec_query($query);
if($insert){
echo "Data inserted successfully !";
}
return $insert;
}
function Update($set,$where=NULL,$marker)
{
foreach($set as $data => $info)
{
$_data[] = $data." = "."'{$info}'";
}
$strRows = implode(",",$_data);
switch($marker)
{
case 0:
$update = "UPDATE $this->table SET
{$strRows}";
break;
case 1:
foreach($where as $rows => $_info)
{
$Rowsinfo[] = $rows."="."'{$_info}'";
}
$strWhere = implode(",", $Rowsinfo);
$update = "UPDATE $this->table SET {$strRows} WHERE {$st
rWhere}";
break;
default:
return false;
}
$res = $this->exec_query($update);
if(is_resource($res)){

return $res;
}
}
function delete($sql)
{
if(!empty($sql)){
$query = "DELETE FROM ".$this->table." WHERE ".$sql;
$del = odbc_conn::Prepare($query);
}
return $del;
}
function results($sql)
{
$count = 0;
$data = array();
$res = odbc_conn::exec_query($sql);
while ($row = db2_fetch_array($res)) {
$data[$count] = $row;
$count++;
}
@odbc_free_result($res);
return $data;
}
function TableResults($sql)
{
if(!empty($sql) && is_string($sql)){
$results = db2_conn::exec_query($sql);
}
return db2_result_all($results);
}
function DropTable($table)
{
return odbc_conn::exec_query("DROP TABLE {$table}");
}
function DataType()
{
return odbc_gettypeinfo($this->conn);
}
function db_name($dbName)
{
$this->db_name = $dbName;
return true;
}
function getDataSkalar($sql) {
$returnData = null;
$stmt = db2_exec($this->conn, $sql, array('cursor' => DB2_SCROLLABLE));
if ($stmt) {
$r = db2_num_rows($stmt);
if ($r > 0) {
$row = db2_fetch_array($stmt,1);
$returnData = $row[0] ;
}
}
return $returnData;
}
} // end of class

?>