TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 06-06-2008, 05:52 PM   #1 (permalink)
The Contributor
 
Join Date: May 2008
Location: Denmark
Posts: 70
Thanks: 3
SpYkE112 is on a distinguished road
Default Mysql resources in array?

Isn't it possible to store multiple mysql connections to different databases in an array? So far i'm able to store them, but it is the same resource in both entries..
My print_r dump looks like this:
Code:
Array
(
    [dev1] => Resource id #7
    [pureftpd] => Resource id #7
)
My code looks like this:
PHP Code:
<?php
//Check if ticket
if (!defined('BACKEND_TICKET'))
{
    die();
}

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

    public function 
connect()
    {
        global 
$p101;
        static 
$host 'localhost';
        static 
$user '';
        static 
$pass '';
        static 
$name = array(
                            
=> 'dev1',
                            
=> 'pureftpd'
                            
);
                                
        switch(
$this->db)
        {
            case 
'mysql':
            {
                foreach(
$name as $val)
                {
                    if (
$this->link[$val] = mysql_connect($host$user$pass))
                    {
                        
                        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!';
                    }
                    
                }
                break;
            }
        }
    }
    
    public function 
query($sql,$return null,$link)
    {
        
$buffer = array();
        switch(
$this->db)
        {
            case 
'mysql':
            {
                switch(
$return)
                {
                    case 
'assoc':
                        {
                            return(
mysql_fetch_assoc(mysql_query($sql,$this->link[$link]))) or die(mysql_error());
                        }
                    case 
'array':
                        {
                            
//return(mysql_fetch_assoc(mysql_query($sql,$this->link[$link])));
                        
}
                    case 
'exec':
                        {
                            if (
mysql_query($sql,$this->link[$link]))
                            {
                                return(
true);
                            } else 
                            {
                                return(
false);
                            }
                        }    
                    default:
                        {
                            return(
mysql_query($sql,$this->link[$link]));
                        }
                
                }
                break;
            }
        }
    }
    
    public function 
__destruct()
    {
        foreach(
$this->link as $key => $val)
        {
            
mysql_close($this->link[$key]);
        }
    }
}

?>
Any ideas how to do this? Or is it just not possible?
SpYkE112 is offline  
Reply With Quote
Old 06-06-2008, 06:38 PM   #2 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Well, that's because you don't pass the fourth parameter to the mysql_connect function as true (it's false by default). Add it and then you'll have different connections.

The definition of the mysql_connect function:

Code:
resource mysql_connect     ([ string $server    [, string $username    [, string $password    [, bool $new_link    [, int $client_flags   ]]]]] )
The $new_link is the parameter you need to set to true if you want separate connections.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
SpYkE112 (06-06-2008)
Old 06-06-2008, 06:46 PM   #3 (permalink)
The Contributor
 
Join Date: May 2008
Location: Denmark
Posts: 70
Thanks: 3
SpYkE112 is on a distinguished road
Default

Thanks a lot, it did the trick :)
SpYkE112 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:34 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design