Welcome Guest, Not a member yet? Register   Sign In
how executed within a helper to $ this-> db-> query ();
#1

[eluser]Lic. Roberto Estupiñán Pérez[/eluser]
hi:
I'm trying to make a helper to restore a database, but I can not run $ this-> db-> query ();
thks.

restore_helper.php
Code:
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

define('MSR_VERSION', '1.0.0');

define('MSR_STRING', 0);
define('MSR_FILE', 1);


class MySQL_Restore
{

  var $queries = array();
  var $error = '';
  var $restore_file_name = '';


  function Execute($param, $mode = MSR_STRING, $is_compressed = true, $split_only = false)
  {
    if ($mode == MSR_FILE){
      if (!($sql = $this->_ReadFromFile($param, $is_compressed)))
          {
            $this->error = 'No puedo leer el fichero de BackUp.'.$param;
            return false;
          }
    }
    else
    {
      $sql = ($is_compressed ? gzuncompress($param) : $param);
    }
    return $this->_Restore($sql, $split_only);
  }

  function _Query($sql){
// HERE ERROR //
    $result = $this->db->query($sql);
    return $result;
  }

  // The logic from phpMyAdmin source
  function _Decompose(&$ret, $sql)
  {
    $sql = rtrim($sql, "\n\r");
    $sql_len = strlen($sql);
    $char = '';
    $string_start = '';
    $in_string = false;
    $nothing = true;
    $time0 = time();
    for ($i = 0; $i < $sql_len; ++$i)
    {
      $char = $sql[$i];
      if ($in_string)
      {
        for (;;)
        {
          $i = strpos($sql, $string_start, $i);
          if (!$i)
          {
            $ret[] = $sql;
            return true;
          }
          elseif ($string_start == '`' || $sql[$i - 1] != '\\')
          {
            $string_start = '';
            $in_string = false;
            break;
          }
          else
          {
            $j = 2;
            $escaped_backslash = false;
            while ($i - $j > 0 && $sql[$i - $j] == '\\')
            {
              $escaped_backslash = !$escaped_backslash;
              $j++;
            }
            if ($escaped_backslash)
            {
              $string_start = '';
              $in_string = false;
              break;
            }
            else
            {
              $i++;
            }
          }
        }
      }
      else if (($char == '-' && $sql_len > $i + 2 && $sql[$i + 1] == '-' &&
        $sql[$i + 2] <= ' ') || $char == '#' ||
        ($char == '/' && $sql_len > $i + 1 && $sql[$i + 1] == '*'))
      {
        $comm_end = ($char == '/' ? '*/' : "\n");
        $i = strpos($sql, $comm_end, $i);
        if ($i === false)
        {
          break;
        }
        if ($char == '/')
        {
          $i++;
        }
      }
      else if ($char == ';')
      {
        $ret[] = array('query' => substr($sql, 0, $i), 'empty' => $nothing);
        $nothing = true;
        $sql = ltrim(substr($sql, min($i + 1, $sql_len)));
        $sql_len = strlen($sql);
        if ($sql_len)
        {
          $i = -1;
        }
        else
        {
          return true;
        }
      }
      else if (($char == '"') || ($char == '\'') || ($char == '`'))
      {
        $in_string = true;
        $nothing = false;
        $string_start = $char;
      }
      elseif ($nothing)
      {
        $nothing = false;
      }
      $time1 = time();
      if ($time1 >= $time0 + 30)
      {
        $time0 = $time1;
        @header('X-pmaPing: Pong');
      }
    }
    if (!empty($sql) && preg_match('@[^[:space:]]+@', $sql))
    {
      $ret[] = array('query' => $sql, 'empty' => $nothing);
    }
    return true;
  }


  function _Restore($sql, $split_only)
  {
    if (!$this->_Decompose($queries, $sql)) {
      return false;
    }
    $errors = '';
    $this->queries = array();
    foreach ($queries as $query) {
      $this->queries[] = $query['query'];
      if (!$split_only) {
        if (!$this->_Query($query['query'])) {
          $errors .= $this->error . '<br /><br />';
        } // if
      } // if
    } // foreach
    if (!empty($errors)) {
      $this_error = $errors;
      return false;
    } // if
    return true;
  }


  function _ReadFromFile($fname, $is_compressed)
  {
      if (file_exists($fname)) {
        if ($is_compressed) {
          $sql = gzfile($fname);
        } else {
          $sql = file($fname);
        } // else
        if ($sql === false) {
          return false;
        } // if
        return implode('', $sql);
     } else return false;
  } // _ReadFromFile

}

?&gt;
#2

[eluser]intractve[/eluser]
You will need to access the current instance first by using

Code:
$CI &= get_instance();

Then from that point forward you use $CI instead of $this
you will have to do this for every function or call

--
or
--
do this from your constructor function.
Code:
$this->ci &= get_instance();

then use $this->ci->db->get(); to access the database library from your class functions.
#3

[eluser]Lic. Roberto Estupiñán Pérez[/eluser]
i'm do this:

function _Query($sql){
$CI &= get_instance();
$this->ci &= get_instance();
$result = $this->ci->db->query($sql);
return $result;
}

but show this error:

Undefined variable: CI
Filename: helpers/restore_helper.php
Line Number: 35
Message: Object of class Admin could not be converted to int
Filename: helpers/restore_helper.php
Line Number: 35
Message: Trying to get property of non-object
Filename: helpers/restore_helper.php
Line Number: 37
Fatal error: Call to a member function query() on a non-object in /var/www/pt/application/helpers/restore_helper.php on line 37
#4

[eluser]intractve[/eluser]
Im sorry if I had not been clear,
Do only this: $CI &= get_instance();
inside a function, and then use it like this
Code:
function _Query($sql){
      $CI &= get_instance();
      $result = $CI->db->query($sql);
  return $result;
  }
#5

[eluser]Lic. Roberto Estupiñán Pérez[/eluser]
thk!!! Work!!!!
but i need change $CI &= get_instance(); by $CI = get_instance();

because what???
#6

[eluser]intractve[/eluser]
What version of PHP are u using?
#7

[eluser]intractve[/eluser]
I have made a typo, it is supposed to be $CI =& get_instance();

if you use just =, you are copying the original object rather than working with the existing one.




Theme © iAndrew 2016 - Forum software by © MyBB