• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dbutils confusion

#1
[eluser]spyro[/eluser]
I wanted to list the databases on a server and started trying to write my own lib until I found dbutil. The only thing that I am confused about is that you can't load a db server instance unless the database is listed in the config. However, the point of listing the databases in my case is to choose. make sense? What am I missing?

#2
[eluser]amites[/eluser]
what have you done so far?

At the very least the DB Utility is going to need login information, giving it a DB to start with will make things easier since having a set db is the case 99% of the time CI is designed to work that way

#3
[eluser]spyro[/eluser]
I started working on a library where I can list the databases by passing sql server info.

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

class Connection {
    
    var $db_type = '';
    var $result = '';
    var $CI ='';
    var $db = '';
    
    function set_server($type, $username, $password, $hostname, $port, $database)
    {
        
        $CI =& get_instance();
        
        if($type == 'mysql' || $type == 'mssql')
        {
        
            $config['hostname'] = $hostname;
            $config['username'] = $username;
            $config['password'] = $password;
            $config['database'] = "";
            $config['dbdriver'] = $type;
            $config['dbprefix'] = "";
            $config['pconnect'] = FALSE;
            $config['db_debug'] = TRUE;
            $config['cache_on'] = FALSE;
            $config['cachedir'] = "";
            $config['char_set'] = "utf8";
            $config['dbcollat'] = "utf8_general_ci";
        
            $db = $CI->load->database($config);
            $util = $CI->load->dbutil($config);
        
            $this->db_type = $type;
        
        }
        
    }

    function query($query_string)
    {
        if (!empty($query_string))
        {
            $this->result = $db->query($query_string);
        
        }
        else
        {
            return false;
        }

    }
    
    function fetch($result="")
    {
         if(empty($this->result))
         {
             $result = $this->result;
             return $result;
         }
         else
         {
             $data = $this->result->result();

             return $data;
         }
        
    }
    
}

?>

#4
[eluser]spyro[/eluser]
Any ideas?

#5
[eluser]spyro[/eluser]
I got a little further in the set_server method. I added the TRUE param to the load->database method which fixed my initial connection issue. Now the problem has moved to the next line. Loading dbutils still returns null.

Code:
function set_server($type, $username, $password, $hostname, $port, $database)
    {
        
        $CI =& get_instance();
        
        if($type == 'mysql' || $type == 'mssql')
        {
        
            
            $config['hostname'] = $hostname;
            $config['username'] = $username;
            $config['password'] = $password;
            $config['database'] = "information_schema";
            $config['dbdriver'] = $type;
            $config['dbprefix'] = "";
            $config['pconnect'] = FALSE;
            $config['db_debug'] = TRUE;
            $config['cache_on'] = FALSE;
            $config['cachedir'] = "";
            $config['char_set'] = "utf8";
            $config['dbcollat'] = "utf8_general_ci";
        
            $db = $CI->load->database($config,TRUE);
            
            $util = $CI->load->dbutil();
        
            $this->db_type = $type;
        
        }
        
    }

#6
[eluser]TheFuzzy0ne[/eluser]
You're loading the database into the variable $db, but loading dbutil on the CI database, which probably doesn't exist.

Try this method instead:
Code:
function set_server($type, $username, $password, $hostname, $port, $database)
    {
        
        $CI =& get_instance();
        
        if($type == 'mysql' || $type == 'mssql')
        {
        
            
            $config['hostname'] = $hostname;
            $config['username'] = $username;
            $config['password'] = $password;
            $config['database'] = "information_schema";
            $config['dbdriver'] = $type;
            $config['dbprefix'] = "";
            $config['pconnect'] = FALSE;
            $config['db_debug'] = TRUE;
            $config['cache_on'] = FALSE;
            $config['cachedir'] = "";
            $config['char_set'] = "utf8";
            $config['dbcollat'] = "utf8_general_ci";
        
            $this->db = $CI->load->database($config,TRUE);
        
            $this->db_type = $type;
        
        }
The above code is untested.

As for loading dbutil (omitted from the above code)... dbutil relies on $this->db being loaded in the CI super object. Do you actually need to use more than one database? If not, you should be loading the database into $this->db in the CI super object instead of it's own variable, and then your problem is solved.

#7
[eluser]spyro[/eluser]
In this case I need to load it into it's own variable because I can be dealing with a number of databases at any time. Ideas on how to do that or do I just need to skip using dbutils for this purpose?

#8
[eluser]TheFuzzy0ne[/eluser]
I would either suggest skipping it, or create your own version within your "Connection" class. It shouldn't be too hard to port. I'm not sure if there will be issues where people can see other people's databases. If the permissions on the databases are correct, I doubt there will be a problem.

Just out of interest. What do you need more than one connection for? If it's only a single connection in any given transaction that needs the dbutil class, then you should load that as the default $this->db in the CI super object, and if the other connection is to your database for validation and other things, I'd suggest that you load that database into a separate variable, and use that. Just load it into the CI super object to make it global. Something like $this->my_db = $CI->load->database($config, TRUE);

If you needed an admin panel, then you'd load your database connection into $this->db in the CISO, as you wouldn't need to use any other connections.

Hope this make sense.

#9
[eluser]spyro[/eluser]
I went ahead and skipped using the dbutils class and just wrote what I needed. I am writing an application that will snapshot and compare both files and databases to detect and show changes which will help developers figure out where things went wrong with deployments or code changes. It also helps with Open source systems like SugarCRM and Wordpress were users are more likely to load new modules in a production instance and break something.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.