如何动态绑定mysqli

I'm in the middle of creating a custom Database Class to suit the requirements of the company i'm developing for. I currently have this:

class DBC {

    protected $Link;
    protected $Results;

    public function __construct($Host = null,$User = null ,$Pass = null,$Database = null){
        if ($Host === null OR $User === null OR $Pass === null OR $Database === null){
            trigger_error("Incorrect Parameters Passed In The Database Link", E_USER_WARNING);
        }
        if (is_string($Host) AND is_string($User) AND is_string($Pass) AND is_string($Database)){
            $this->Link = new mysqli($Host,$User,$Pass,$Database);
        }else{
            trigger_error("Expecting String(s), Array passed in one or more connection parameters",E_USER_ERROR);
        }
    }
    public function Query ($Query,$Params){
        $Query = $this->Link->prepare($Query);
        $Query->bind_param();
    }


}

Now.. I'm having a problem with how to sucessfully bind the parameters to the prepared statement.. For example, A Query will be submitted with this:

$DB = new DBC("Host","User","pass","database");
$DB->Query("SELECT * FROM Test WHERE Col=?",array("SearchCriteria"));

I've hit a block with figuring out how to bind_param and bind_result based on the results. A more clear insight is the normal procedure of MySQLi:

$SearchCriteria = "String";
$Query = $Database->prepare("SELECT * FROM Test WHERE Col=?");
$Query->bind_param('s',$SearchCriteria);
$Query->execute(); 
$Query->bind_results(/* Variables to match the column set */);
$Query->fetch();
$Query->close();

How can I bind the results and params to the prepared statement?

Below are copies of functions I use in a class that extends the mysqli class which do what you are asking.

function bind_placeholder_vars(&$stmt,$params,$debug=0) {
    // Credit to: Dave Morgan
    // Code ripped from: http://www.devmorgan.com/blog/2009/03/27/dydl-part-3-dynamic-binding-with-mysqli-php/
    if ($params != null) {
        $types = '';                        //initial sting with types
        foreach ($params as $param) {        //for each element, determine type and add
            if (is_int($param)) {
                $types .= 'i';              //integer
            } elseif (is_float($param)) {
                $types .= 'd';              //double
            } elseif (is_string($param)) {
                $types .= 's';              //string
            } else {
                $types .= 'b';              //blob and unknown
            }
        }

        $bind_names = array();
        $bind_names[] = $types;             //first param needed is the type string
                                // eg:  'issss'

        for ($i=0; $i<count($params);$i++) {    //go through incoming params and added em to array
            $bind_name = 'bind' . $i;       //give them an arbitrary name
            $$bind_name = $params[$i];      //add the parameter to the variable variable
            $bind_names[] = &$$bind_name;   //now associate the variable as an element in an array
        }

        if ($debug) {
            echo "\$bind_names:<br />
";
            var_dump($bind_names);
            echo "<br />
";
        }
        //error_log("better_mysqli has params ".print_r($bind_names, 1));
        //call the function bind_param with dynamic params
        call_user_func_array(array($stmt,'bind_param'),$bind_names);
        return true;
    }else{
        return false;
    }
}



function bind_result_array($stmt, &$row) {
    // Credit to: Dave Morgan
    // Code ripped from: http://www.devmorgan.com/blog/2009/03/27/dydl-part-3-dynamic-binding-with-mysqli-php/
    $meta = $stmt->result_metadata();
    while ($field = $meta->fetch_field()) {
        $params[] = &$row[$field->name];
    }
    call_user_func_array(array($stmt, 'bind_result'), $params);
    return true;
}

However, it sounds like you are doing something similar to what I have already done and have been using in many projects for a while now. Copy the contents of this pastebin (better_mysqli.php) into a new file and name it 'better_mysqli.php'

Then use it in your php program like so:

// include the class
include_once('better_mysqli.php');


// instantiate the object and open the database connection
$mysqli = new better_mysqli('yourserver.com', 'username', 'password', 'db_name');
if (mysqli_connect_errno()) {
      die("Can't connect to MySQL Server. Errorcode: %s
", mysqli_connect_error()), 'error');

}

// do a select query
$sth = $mysqli->select('select somecol, othercol from sometable where col1=? and col2=?', $row, array('col1_placeholder_value', 'col2_placeholder_value'));

while ($sth->fetch()) {
    echo "somecol: ". $row['somecol'] ."<br />
";
    echo "othercol: ". $row['othercol'] ."<br />
";
}

// the nice thing about this class is that the statement is only prepared once so if you use it again the already prepared statement is automatically used:

// do another select query with different placeholder values
$sth = $mysqli->select('select somecol, othercol from sometable where col1=? and col2=?', $row, array('other_col1_placeholder_value', 'other_col2_placeholder_value'));

while ($sth->fetch()) {
    echo "somecol: ". $row['somecol'] ."<br />
";
    echo "othercol: ". $row['othercol'] ."<br />
";
}

// the class supports the following methods:  select, update, insert, and delete


// example delete:
$mysqli->delete('delete from sometable where col1=?', array('placeholder_val1'));