PHP类动态执行select查询(MySQLI和PHP)

时间:2014-12-11 01:56:44

标签: php oop mysqli

我在执行dinamicly select查询时遇到问题,'选择'功能不起作用,你能帮帮我吗?

这是korisnici表: korisnik_id korisnik_ime korisnik_lozinka korisnik_grupa korisnik_datum korisnik_salt

这里是DB.php类,类的末尾是Select函数。

<?php
class DB{
    private static $_instance = null;
    private $_stmt,$_query,$_error=false,$_results,$count=0;

    public function __construct() {
        try{

            $this-> _stmt = new mysqli(Config::get('mysql/host'),Config::get('mysql/username'),Config::get('mysql/password'),Config::get('mysql/db'));
            if (mysqli_connect_errno()) {
                printf("Connect failed: %s\n", mysqli_connect_error());
                exit();
            }

        } catch (Exception $ex) {

        }

    }

    public static function getInstance(){

        if(!isset(self::$_instance)){
            self::$_instance = new DB();
        }
        return self::$_instance;
    }


    public function Select($query, $paramString = ''){
        $stmt = $this->_stmt->prepare($query);
        if (func_num_args() > 2){
            $parameters = func_get_args();
            array_shift($parameters); // remove the query from the list
            // Array needs to be bound by reference
            foreach ($parameters as $key=>&$value) {
                $parameters[$key] = &$value;
            }
            call_user_func_array(array($stmt, "bind_param"), $parameters);
        }
        $stmt->execute();
        $this->result = $stmt->get_result();
        $stmt->close();
        print_r($this->result);

    }
}
?>

这是index.php文件:

<?php
require_once 'base/init.php';
DB::getInstance() -> Select('SELECT korisnik_id FROM korisnici WHERE korisnik_ime= ?', array('s','Alex'));

1 个答案:

答案 0 :(得分:1)

DB::Select()期望绑定参数是单独的参数,而不是数组。所以你应该把它称为:

DB::getInstance() -> Select('SELECT korisnik_id FROM korisnici WHERE korisnik_ime= ?', 's', 'Alex');