reporting service odbc mysql_odbc_fetch_array

[#12]

russ at russprince dot com [2004-11-11 18:57:02]

I really liked Ryan's example so I took it another step and added a recordset class to work with the connection class.  I made slight alterations to the original code as well.  Also note the recordset class takes advantage of php5's __get property function...

class odbcRecordset {

var $recordcount;

var $currentrow;

var $eof;

var $recorddata;

var $query;

function odbcConnection(){

$this->recordcount = 0;

$this->recorddata = 0;

}

function SetData( $newdata, $num_records, $query ) {

$this->recorddata = $newdata;

$this->recordcount = $num_records;

$this->query = $query;

$this->currentrow = 0;

$this->set_eof();

}

function set_eof() {

$this->eof = $this->currentrow >= $this->recordcount;

}

function movenext()  { if ($this->currentrow recordcount) { $this->currentrow++; $this->set_eof(); } }

function moveprev()  { if ($this->currentrow > 0)                  { $this->currentrow--; $this->set_eof(); } }

function movefirst() { $this->currentrow = 0; set_eof();                                               }

function movelast()  { $this->currentrow = $this->recordcount - 1;  set_eof();                         }

function data($field_name) {

if (isset($this->recorddata[$this->currentrow][$field_name])) {

$thisVal = $this->recorddata[$this->currentrow][$field_name];

} else if ($this->eof) {

die("Error! eof of recordset was reached");

} else {

die("Error! Field " . $field_name . " was not found in the current recordset from query:
$this->query");

}

return $thisVal;

}

function __get($field_name) {

return $this->data($field_name);

}

}

class odbcConnection {

var $user;  //Username for the database

var $pass; //Password

var $conn_handle; //Connection handle

var $temp_fieldnames; //Tempory array used to store the fieldnames, makes parsing returned data easier.

function odbcConnection(){

$this->user = "";

$this->pass = "";

}

function open($dsn,$user,$pass){

$handle = @odbc_connect($dsn,$user,$pass,SQL_CUR_USE_ODBC) or

die("Error! Couldn't Connect To Database. Error Code:  ".odbc_error());

$this->conn_handle = $handle;

return true;

}

function &execute($query){

//Create a temp recordset

$newRS = new odbcRecordset;

$thisData = "";

$res = @odbc_exec($this->conn_handle,$query) or

die("Error! Couldn't Run Query:
" . $query . "
Error Code:  ".odbc_error());

unset($this->temp_fieldnames);

$i = 0;

$j = 0;

$num_rows = 0;

// only populate select queries

if (stripos($query, 'select ') !== false) {

while(odbc_fetch_row($res)) {

$num_rows++;

//Build tempory

for ($j = 1; $j <= odbc_num_fields($res); $j++) {

$field_name = odbc_field_name($res, $j);

$this->temp_fieldnames[$j] = $field_name;

$ar[$field_name] = odbc_result($res, $field_name) . "";

}

$thisData[$i] = $ar;

$i++;

}

}

//populate the recordset and return it

$newRS->SetData( $thisData, $num_rows, $query );

return $newRS;

}

}

%>

usage is pretty simple:

$con = new odbcConnection

$con->open("dsn","user","pass")

$sql = "select bar from foo";

$rs = $con->execute($sql);

if (!$rs->eof) {

print $rs->data("bar");

// or //

print $rs->bar;

}

while (!$rs->eof) {

// blah blah code

$rs->movenext();

}

%>

Works pretty well, but I haven't thoughly tested it yet.

Code can be dl'd here:

http://www.russprince.com/odbc_functions.zip

Cheers,

Russ

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值