Calver Posted September 19, 2009 Share Posted September 19, 2009 Hello, I've only been learning PHP and MySQL for a short time, and managed to sort my problems out so far. However, this one has me stumped. I'm trying to build a wrapper for my database, and have got this far. I'll be passing the parameters eventually, but just keeping it simple for testing. class DB { private $conn; function __construct() { $this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASSWORD, DB_NAME) or die('There was a problem connecting to the database.'); } function TestA() { $query = "SELECT * FROM users WHERE username = ? AND firstname = ?"; $stmt = $this->conn->prepare($query); $var1 = "stan"; $var2 = "Stanley"; $stmt->bind_param('ss', $var1, $var2); $stmt->execute(); if ($stmt->fetch()) return true; return false; } function TestB() { $query = "SELECT * FROM users WHERE username = ? AND firstname = ?"; $stmt = $this->conn->prepare($query); $params = array('ss', 'stan', 'Stanley'); call_user_func_array(array($stmt, 'bind_param'), $params); $stmt->execute(); if ($stmt->fetch()) return true; return false; } Function TestA works just fine and returns true. Function TestB is intended to accept a variable number of parameters, but 'call_user_func_array...' is generating this error: "Warning: Parameter 2 to mysqli_stmt::bind_param() expected to be a reference, value given..." What am I doing wrong? [using WAMP with PHP 5.3.0 and MySQL 5.1.36] Quote Link to comment https://forums.phpfreaks.com/topic/174829-solved-problem-with-mysqli-and-bind_param/ Share on other sites More sharing options...
corbin Posted September 20, 2009 Share Posted September 20, 2009 Hrmmmmm odd. I just looked on the manual page for call_user_func_array and for some reason it appears that the array is passed by value. Usually things are passed by value, but they're references until it is edited. This is a ghetto hacked suggested by someone: $params = array('ss', 'stan', 'Stanley'); $tmp = array(); foreach($params as $key => $value) $tmp[$key] = &$params[$key]; call_user_func_array(array($stmt, 'bind_param'), $tmp); Quote Link to comment https://forums.phpfreaks.com/topic/174829-solved-problem-with-mysqli-and-bind_param/#findComment-921528 Share on other sites More sharing options...
Calver Posted September 20, 2009 Author Share Posted September 20, 2009 Hrmmmmm odd. I just looked on the manual page for call_user_func_array and for some reason it appears that the array is passed by value. Usually things are passed by value, but they're references until it is edited. This is a ghetto hacked suggested by someone: $params = array('ss', 'stan', 'Stanley'); $tmp = array(); foreach($params as $key => $value) $tmp[$key] = &$params[$key]; call_user_func_array(array($stmt, 'bind_param'), $tmp); Brilliant! Thank you very much for your help. This works just fine and I can now move on Quote Link to comment https://forums.phpfreaks.com/topic/174829-solved-problem-with-mysqli-and-bind_param/#findComment-921677 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.