View Single Post
Old 06-09-2008, 04:32 PM   #1 (permalink)
SpYkE112
The Contributor
 
Join Date: May 2008
Location: Denmark
Posts: 70
Thanks: 3
SpYkE112 is on a distinguished road
Default Multiple mysql connections doesn't seem to work :S

I got the following class, where i wan't to use multiple database connections, but it is somehow broke. It will only make one connection :/
PHP Code:
<?php
//Check if ticket
if (!defined('BACKEND_TICKET'))
{
    die();
}

class 
database extends p101
{
    public 
$link = array();
    public 
$db 'mysql';

    public function 
connect()
    {
        static 
$host 'localhost';
        static 
$user 'SpYkE112';
        static 
$pass '';
        
        static 
$name = array(
                            
=> 'dev1',
                            
=> 'pureftpd'
                            
);
        global 
$p101;
                            
        switch(
$this->db)
        {
            case 
'mysql':
            {    
                foreach(
$name as $val)
                {
                    if (
$this->link[$val] = mysql_connect($host$user$passtrue))
                    {
                        if (
mysql_select_db($val$this->link[$val]))
                        {
                            return(
true);
                        } else
                        {
                            
$p101->error[] = 'Unable to select database: ' $val '!';
                        }
                        
                    } else 
                    {
                        
$p101->error[] = 'Unable to connect to database! ' $val '!';
                    }
                }
                
                break;
            }
        }
    }
    
    public function 
query($sql,$type null,$dbname null)
    {    
        global 
$p101;
        
//if (empty($dbname))
        //{
        //    $dbname = $this->name[0];
        //}
        
        
switch($type)
        {
            case 
'assoc':
                {
                    if (
$query mysql_query($sql,$this->link[$dbname]))
                    {
                        return(
mysql_fetch_assoc($query));
                    } else
                    {
                        die(
mysql_error());
                    }
                }
            case 
'array':
                {
                    if (
$query mysql_query($sql,$this->link[$dbname]))
                    {
                        return(
mysql_fetch_assoc($query));
                    } else
                    {
                        die(
mysql_error());
                    }                    
                }
            default:
                {
                    if (
$query mysql_query($sql,$this->link[$dbname]))
                    {
                        return(
$query);
                    } else
                    {
                        die(
mysql_error());
                    }                    
                }
        }
    }
    
    public function 
__destruct()
    {
        foreach(
$this->link as $key => $val)
        {
            
mysql_close($this->link[$key]);
        }
    }
}

?>
If i print_r's the link variable i get this:
Code:
Array
(
    [dev1] => Resource id #7
)
SpYkE112 is offline  
Reply With Quote