Пример #1
0
 /**
  * Executes a series of queries joined togather by a ';'. Once executed
  * we look into the options array for each dataset using its index
  * (basically the number it appeared in the concatenated string) looking
  * for a key to replace the index with and a callback to use on each row
  * of the dataset
  *
  * @param	mysqli	$drv		mysqli driver
  * @param	string	$sql		sql statements joined into one string
  * @param	array	$options	map used to convert dataset indexes into
  *								meaningful keys and hold callbacks
  * @return array	list of DbResponseInterface
  */
 public function execute(mysqliDriver $drv, $sql, array $createResponse, array $options = array())
 {
     if (!is_callable($createResponse)) {
         $errCode = 'AF_ERR_BAD_CALLBACK';
         $errTxt = 'reponse callback is not valid';
         $error = new Error(-1, $errCode, $errTxt);
         return call_user_func($createResponse);
     }
     /* 
      * -1 key indicated the loop never ran and this most likely a 
      * syntax error. 
      */
     if (!$drv->multi_query($sql)) {
         $error = new Error(-1, $drv->errno, $drv->error, $drv->sqlstate);
         return call_user_func($createResponse, $error);
     }
     /* index for each query, this is mapped to the result keys */
     $idx = 0;
     $data = array();
     do {
         /*
          * check for the existence of all available options
          */
         $isOption = isset($options[$idx]);
         $isResultKey = $isOption && isset($options[$idx]['result-key']);
         $isCallback = $isOption && isset($options[$idx]['callback']);
         $resultKey = $idx;
         if ($isResultKey) {
             $resultKey = $options[$idx]['result-key'];
         }
         $callback = null;
         if ($isCallback) {
             $callback = $options[$idx]['callback'];
         }
         /*
          * Returns a buffered result object or false if an error occured
          */
         $driverResult = $drv->store_result();
         if (!$driverResult) {
             $error = new Error($resultKey, $drv->errno, $drv->error, $drv->sqlstate);
             $data[$resultKey] = call_user_func($createResponse, $error);
             return $data;
         }
         $result = new Result($driverResult);
         $fetched = $result->fetchAllData(MYSQLI_ASSOC, $callback);
         $data[$resultKey] = call_user_func($createResponse, $fetched);
         $isMore = $drv->more_results();
         if ($isMore) {
             $drv->next_result();
             $idx++;
         }
     } while ($isMore);
     return $data;
 }
Пример #2
0
 public function close()
 {
     if (is_object(self::$db_link)) {
         self::$db_link->close();
         self::$db_link = null;
         $this->link = null;
     }
 }
Пример #3
0
 /**
  * Connects to a MySQL database
  *
  * @access public
  * @return  void
  */
 public function connect()
 {
     try {
         self::$_link = new MySQLi($this->host, $this->user, $this->password, $this->database);
         if (self::$_link->connect_error) {
             die('Connect Error (' . self::$_link->connect_errno . ') ' . self::$_link->connect_error);
         }
         if (mysqli_connect_error()) {
             die('Connect Error (' . mysqli_connect_errno() . ') ' . mysqli_connect_error());
         }
         $this->select();
     } catch (Exception $e) {
         die($e->getMessage());
     }
 }