farban Posted June 25, 2011 Share Posted June 25, 2011 Hello Got a problem where I want to get prepared statements to work through adding parameters, something like this <?php class Prepared_Statement extends Handle_DB { private $query; private $bind_result; private $stmt; public function set_PS_query($query) { $this->query = $query; } public function setup_PS($bindresult) { $this->stmt = parent::connect_DB()->stmt_init(); $this->stmt->prepare($this->query); $this->stmt->execute(); $this->stmt->bind_result($bindresult); } public function show_PS_results($result) { while($this->stmt->fetch()) { return $result; } } public function close_PS() { $this->stmt->close(); } } $preS = new Prepared_Statement(); $preS->set_PS_query("SELECT user_name FROM usertest"); $preS->setup_PS("user_name"); echo $preS->show_PS_results("user_name"); $preS->close_PS(); ?> The problem is that it throws up Undefined variable: user_name errors when i run the script. I know why this is the case, how can I pass a variable into the prepared statement without it saying its undefined?? Link to comment https://forums.phpfreaks.com/topic/240365-how-to-get-prepared-statements-to-work-through-oop/ Share on other sites More sharing options...
redixx Posted June 25, 2011 Share Posted June 25, 2011 Try something like this: if (is_int($param)) { $type = 'i'; } else if (is_float($param)) { $type = 'd'; } else { $type = 's'; } call_user_func_array(array($this->stmt,'bind_param'),array_merge(array($type),$param)); Link to comment https://forums.phpfreaks.com/topic/240365-how-to-get-prepared-statements-to-work-through-oop/#findComment-1234664 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.