我需要实现一个可以绑定一个或多个参数的方法select()
和另一个将结果返回到index.php的方法。
从index.php调用所需的代码:
echo $this->_results -> korisnik_id;
这是需要实现select()
的数据库类。当前Select()
函数接受一个参数...
DB::getInstance() -> Select('SELECT korisnik_id FROM korisnici WHERE korisnik_ime= ?', 's', 'Alex');
......但不是更多:
DB::getInstance() -> Select('SELECT korisnik_id FROM korisnici WHERE korisnik_ime= ? AND korisnik_grupa= ?','si', 'Alex', '1');
全班:
<?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->_results = $stmt->get_result();
$stmt->close();
$this->_results = $this->_results -> fetch_object();
echo $this->_results -> korisnik_id;
}
}
?>
答案 0 :(得分:0)
我认为你非常接近。
在当前的forach循环中,这样的事情怎么样:
foreach ($parameters as $key=>&$value) {
$this->_stmt->bindParam ($key, $value, PDO::PARAM_STR);
}