我需要实现一个方法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;
}
}
?>