Jump to content

[SOLVED] Problem with Mysqli and bind_param


Calver

Recommended Posts

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]

 

 

 

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);

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  :)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.