Voting

: six minus six?
(Example: nine)

The Note You're Voting On

bravo1_r at hotmail dot com
3 years ago
Important when using classes:
You must call db2_execute() in the same scope as where you define / set / bind the variables.
For example:

<?php
class DB2Class {
public
$conn;
private
$usr = 'user';
private
$pss = 'password';
private
$cat = 'catalog';
public function
db2_conn(){
$conn = db2_connect($this->cat,$this->usr,$this->pss);
if(!
$conn)
throw new
Exception(db2_conn_errormsg());
$this->conn = $conn;
}
public function
db2_prep($sql){
if(!
$stmt = db2_prepare($this->conn, $sql)){
throw new
Exception($sql . " " . db2_stmt_errormsg());
return
false;
}
return
$stmt;
}
public function
db2_exec($stmt){
if(!
db2_execute($stmt))
throw new
Exception(db2_stmt_errormsg($stmt));
}
}

/* This will NOT work */
function bindtest(){
try {
$db2 = new DB2Class;
$db2->db2_conn();
$stmt = $db2->db2_prep("SELECT * FROM TABLE WHERE FIELD = ?");
$field = 'value';
db2_bind_param($stmt, 1, "field", DB2_PARAM_IN);
$db2->db2_exec($stmt); // Results in Unbound Variable Error
while($row = db2_fetch_assoc($stmt))
var_dump($row);
} catch(
Exception $e) {
error_log( $e->getMessage () );
}
}

/* This will work */
function bindtest(){
try {
$db2 = new DB2Class;
$db2->db2_conn();
$stmt = $db2->db2_prep("SELECT * FROM TABLE WHERE FIELD = ?");
$field = 'value';
db2_bind_param($stmt, 1, "field", DB2_PARAM_IN);
if(!
db2_execute($stmt))
throw new
Exception(db2_stmt_errormsg($stmt));
while(
$row = db2_fetch_assoc($stmt))
var_dump($row);
} catch(
Exception $e) {
error_log( $e->getMessage () );
}
}

?>

<< Back to user notes page

To Top