note 126536 added to function.db2-bind-param

From: Date: Mon, 25 Oct 2021 13:00:14 +0000
Subject: note 126536 added to function.db2-bind-param
Groups: php.notes 
Request: Send a blank email to php-notes+get-217115@lists.php.net to get a copy of this message
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 () ); } } ?> ---- Server IP: 45.112.84.4 Probable Submitter: 45.112.85.242 (proxied: 185.253.177.9) ---- Manual Page -- https://php.net/manual/en/function.db2-bind-param.php Edit -- https://main.php.net/note/edit/126536 Del: integrated -- https://main.php.net/note/delete/126536/integrated Del: useless -- https://main.php.net/note/delete/126536/useless Del: bad code -- https://main.php.net/note/delete/126536/bad+code Del: spam -- https://main.php.net/note/delete/126536/spam Del: non-english -- https://main.php.net/note/delete/126536/non-english Del: in docs -- https://main.php.net/note/delete/126536/in+docs Del: other reasons-- https://main.php.net/note/delete/126536 Reject -- https://main.php.net/note/reject/126536 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#217115) next »