Meine Klasse:
PHP-Code:
<?php
class MyDb extends mysqli {
private $mydb_server = "localhost";
private $mydb_user = "root";
private $mydb_pass = "";
private $mydb_char = "UTF-8";
private $mydb_base = "dbToUse";
private $mydb_Obj;
private $mydb_publicParamsArr;
/**
* This function setup the parameters for the database connection and uses the connect method for open a connection
* @param string $charset
* @param string $database
* @param string $user
* @param string $pass
* @param string $server
* @return boolean
*/
public function __construct($charset=null,$database=null,$user=null,$pass=null,$server=null) {
$this->mydb_publicParamsArr=array('mydb_char','mydb_base');
if ($charset!==null) {
$this->mydb_char=$db_charset;
}
if ($database!==null) {
$this->mydb_char=$database;
}
if ($user!==null) {
$this->mydb_char=$user;
}
if ($pass!==null) {
$this->mydb_char=$pass;
}
if ($server!==null) {
$this->mydb_char=$server;
}
if (!$this->mydb_Obj=new mysqli($this->mydb_server,$this->mydb_user,$this->mydb_pass,$this->mydb_base)) {
$paramArr=array(); //TODO: Fill this array whith connection data
logError('mydbConnect1', $paramArr);
}
}
public function doQuery($type,$params) {
$results='';
$sql=$this->buildQuery($type,$params);
if ($type=="s") {
//SELECT PREPARE STATEMENT
if (!$this->mydb_Obj->connect_errno) {
if ($q=$this->mydb_Obj->stmt_init()) {
createLogFileForTesting("testok.txt",$sql."\nt:".$type."\np:".serialize($params));
} else {
createLogFileForTesting("testok.txt",$this->mydb_Obj->error);
}
} else {
createLogFileForTesting("testfailed.txt",$sql."\nt:".$type."\np:".serialize($params));
return 'FAILED';
}
if ($q->prepare($sql)) {
$bindings=explode(BREAKER,$this->bindParams($params,'where','whereType'));
$bindingType=$bindings[0];
$bindingValue=$bindings[1];
$paramArr=unserialize($bindings[2]);
$bind_names=$this->bindNames($q,$paramArr, $bindingType);
$q->execute();
$bind_result=$this->bindResult($q,$q->result_metadata());
if ($fields=$this->bindResult($q,$q->result_metadata())) {
$results=$this->fetchResults($q,$fields);
} else {
$paramArr=array(); //TODO: Fill this Array with request Data
logError('MyDbRequestFailed1', $paramArr);
}
$q->close();
} else {
$paramArr=array('sql=>'.$sql); //TODO: Fill this array with helping things
logError("MyDbSelectPreparationFailed", $paramArr);
}
} elseif ($type=="u") {
$q=$this->mydb_Obj->stmt_init();
if ($q->prepare($sql)) {
//BINDEN VON SET UND WHERE ZUSAMMENFASSEN!!
$bindings=explode(BREAKER,$this->bindParams($params,'set','setType'));
$bindingType=$bindings[0];
$bindingValue=$bindings[1];
$paramArr=unserialize($bindings[2]);
$bindings2=explode(BREAKER,$this->bindParams($params,'where','whereType'));
$bindingType.=$bindings2[0];
$bindingValue.=','.$bindings2[1];
$paramArr=array_merge($paramArr,unserialize($bindings2[2]));
$bind_names=$this->bindNames($q,$paramArr, $bindingType);
$q->execute();
$q->close();
} else {
$paramArr=array('sql=>'.$sql); //TODO: Fill this array with helping things
logError("MyDbUpdatePreparationFailed", $paramArr);
return "MyDbUpdatePreparationFailed";
}
}
return $results;
}
/**
* This method creates the binding Parameters for prepared statements
* @param array $params
* @param string $key1
* @param string $key2
* @return string
*/
private function bindParams($params,$key1,$key2) {
$e=0;
$cWhere=count($params[$key1]);
$bindingType="";
$bindingValue="";
$paramArr=array();
foreach ($params[$key1] as $key => $value) {
$bindingType.=$params[$key2][$e];
$bindingValue.=$value;
$paramArr[]=$value;
if ($cWhere>1) {
$bindingValue.=', ';
}
$e++;
$cWhere--;
}
return $bindingType.BREAKER.$bindingValue.BREAKER.serialize($paramArr);
}
/**
* This method creates the bindingnames string for prepared statements
* @param object $q
* @param array $paramArr
* @param string $bindingType
* @return array
*/
private function bindNames($q,$paramArr,$bindingType) {
$bind_names[]=$bindingType;
for ($i=0; $i<count($paramArr);$i++) {
$bind_name = 'bind' . $i;
$$bind_name = $paramArr[$i];
$bind_names[] = &$$bind_name;
}
call_user_func_array(array($q,'bind_param'),$bind_names);
return $bind_names;
}
/**
* This method bind the parameters for the fetch results for prepared statements
* @param object $q
* @param object $meta
* @return array
*/
private function bindResult($q,$meta) {
$var='';
$fields=array();
while ($field = $meta->fetch_field()) {
$var = $field->name;
$$var = null;
$fields[$var] = &$$var;
}
call_user_func_array(array($q,'bind_result'),$fields);
return $fields;
}
/**
* This method build the needed SQL query
* @param string $type
* @param array $params
* @return string
*/
private function buildQuery($type,$params) {
switch ($type) {
case "s":
//SELECT
$sql='SELECT '.$params['what'].' FROM '.$params['table'];
if (in_array("innerJoin", $params)) {
$sql.=' INNER JOIN '.$params['innerJoin'].' ON '.$params['innerJoinOn'];
}
if (array_key_exists("where", $params)) {
$sql.=' WHERE ';
$e=0;
$cWhere=count($params['where']);
foreach ($params['where'] as $key => $value) {
$sql.=$key.'=?';
if ($cWhere>1) {
$sql.=' AND ';
}
$e++;
$cWhere--;
}
}
if (in_array("sort", $params)) {
$sql.=' SORT BY '.$parmas['sort'];
}
if (in_array('limit', $params)) {
$sql.=' LIMIT '.$params['limit'];
}
break;
}
return $sql;
}
}
Mein Problem:
Ich greife auf die Klasse von weiteren Klassen, so auch der Klasse User zu. Nachdem sich der Nutzer erfolgreich angemeldet hat (hier funktioniert obriges einwandfrei), möchte ich auf einer weiteren Seite Abfragen, ob der Nutzer entsprechende Rechte hat.
Hierzu gibt es folgende Methode in der Class User:
PHP-Code:
public function hasRight($right) {
$whereArr=array("userName"=>$this->userName);
$paramsArr=array(
"what"=>"userRights",
"table"=>"tbl_user",
"whereType"=>$this->db->genTypes($whereArr),
"where"=>$whereArr
);
$res=$this->db->doQuery("s",$paramsArr);
if (count($res)==1) {
$rightArr=unserialize($res[0]['userRights']);
if ((in_array($right,$rightArr)) || (in_array('*',$rightArr))) {
return true;
} else {
return false;
}
} else {
logError('rightCheck2', array('class'=>'User'));
return false;
}
}
Bei der Abfrage erscheint nun aber:
Code:
Warning: mysqli::stmt_init(): Couldn't fetch mysqli in C:\entwicklung\xampp_travel\htdocs\skysimadmin\inc\class\MyDb.class.inc.php on line 53
Fatal error: Call to a member function prepare() on null in C:\entwicklung\xampp_travel\htdocs\skysimadmin\inc\class\MyDb.class.inc.php on line 62