TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Multiple mysql connections doesn't seem to work :S (http://www.talkphp.com/advanced-php-programming/2937-multiple-mysql-connections-doesnt-seem-work-s.html)

SpYkE112 06-09-2008 03:32 PM

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
)


Salathe 06-09-2008 03:46 PM

On line 35 (return(true);) the method returns the first time that a database is selected which stops further execution of the foreach loop (and any other code in the method).

Take that line out, or replace it with (a useless, as the code is at the moment) continue; to continue looping through all of the $names.

SpYkE112 06-09-2008 04:22 PM

I changed the code to this:
PHP Code:

    public function connect()
    {
        static 
$host 'localhost';
        static 
$user 'SpYkE112';
        static 
$pass '';
        static 
$name = array(=> 'dev1'=> 'pureftpd');
        
        for(
$i 0$i sizeof($name); $i++)
        {
            if (
$this->link[$name[$i]] = mysql_connect($host,$user,$passtrue))
            {
                if (!
mysql_select_db($name[$i],$this->link[$name[$i]]))
                {
                    
$p101->error['undbs'] = 'Unable to select database: ' $name[$i];
                    return(
false);
                } else {
                    continue;
                }
            } else {
                
$p101->error['uncdb'] = 'Unable to connect database: ' $name[$i];
                return(
false);
            }
        }
    } 

But now it just returns an empty array :/

Aaron 06-10-2008 02:29 AM

I must be missing something here, but why would you need more than one?

delayedinsanity 06-10-2008 03:28 AM

I haven't tested this, but maybe you could try it?

PHP Code:

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) {
                
$this->link[$val] = mysql_connect($host$user$passtrue);
                if (!
$this->link[$val]) {
                    
$p101->error[] = 'Unable to connect to database! ' $val '!';
                    return 
false;

                } else {
                    if (!
mysql_select_db($val$this->link[$val])) {
                        
$p101->error[] = 'Unable to select database: ' $val '!';
                        return 
false;
                    }
                    
                } 
// if
            
// foreach
    
// switch




delayedinsanity 06-10-2008 03:56 AM

I just noticed you removed the switch in your second code block. So I did too, and tidied it up a bit more. *shrug* Probably pointless if my last one didn't work, but I'm listening to CSPAN right now, so I kind of just did it in a daze.

PHP Code:

public function __construct (p101 $p101$szHost$szUser$szPass$aDBs) {

    foreach(
$aDBs as $value) {
        
$this->link[$value] = mysql_connect($szHost$szUser$szPasstrue);

        if (!
$this->link[$value]) {
            
$p101->error[] = 'Unable to connect to database: '.mysql_error();
            return 
false;

        } else {
            if (!
mysql_select_db($value$this->link[$value])) {
                
$p101->error[] = 'Unable to select database: '.$value.'. '.mysql_error();
                return 
false;
            }
            
        } 
// if

    
// foreach



...then if you want you could deliver your host/user/pass dynamically or store them in a configuration constant somewhere and use it something like so:

PHP Code:

$aDBs = array(
            
'dev1',
            
'pureftpd'
            
);

$pDatabase = new database($p101'localhost''SpYkE112'''$aDBs);

// or with constants defined elsewhere

$pDatabase = new database($p101DB_HOSTDB_USERDB_PASS, array('dev1''pureftpd')); 


SpYkE112 06-10-2008 12:16 PM

That worked thanks a lot! ^^


All times are GMT. The time now is 04:52 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0