My Question is if there is a simple solution to save wich array of DB_Infos(Online/Offline) should be used.
//Sorry for so much code, gonna clean it up after posting //Btw the origin source of this connect function is from MyBB, where there is an implementation for different write and read functions. I didn't included that yet.
I would like to create a PHP where you don't have to manually switch between the Online Server Connection and the Offline(XAMPP) ones. I used to have a PHP in a class (so it's only once executed per page load)
function connect($config) { $connections['read'] = $config; $connections['write'] = $connections['read']; $this->db_encoding = $config['db']['encoding']; // Actually connect to the specified servers foreach(array('read', 'write') as $type) { // Loop-de-loop foreach($connections[$type] as $single_connection) { $link = "{$type}_link"; $read_link = @mysqli_connect($single_connection['hostname'], $single_connection['username'], $single_connection['password'], $single_connection['db']); // Successful connection? break down brother! if($read_link) { $this->read_link = $read_link; $this->write_link = $read_link; break; } } } if (!isset($write_link)){ $write_link = $read_link; } return $read_link; } config.php:
$config['db']['multiple']['active'] = true; $config['db']['multiple']['var'] = "2"; $config['db']['separate'] = false; //separate in read and write? not supported yet //configuration of database $config['db']['type'] = 'mysqli'; $config['db']['db'] = 'd1'; $config['db']['prefix'] = ''; $config['db']['hostname'] = 'localhost'; $config['db']['username'] = 'root'; $config['db']['password'] = ''; $config['db']['encoding'] = 'utf8'; //configuration fro multiple database (not recommended) $config['db2']['type'] = 'mysqli'; $config['db2']['db'] = 'd2'; $config['db2']['prefix'] = ''; $config['db2']['hostname'] = 'h2'; $config['db2']['username'] = 'u2'; $config['db2']['password'] = 'ΓΌ2'; $config['db2']['encoding'] = 'utf8'; This script works fine, but you always get the run out time when your on the 2nd server (the online one in this example) which creates a delay of 10ms (because it tries to connect to the 1st server) each time. So I created a script trying to solve the 10ms problem, my solution ended up to be a mess of php and xml. The server.XML file was to store wich server is active, and if things change it changes the server.XML
My PHP version to fix the 10ms delay: function connect($config) {
//if multi if($config['db']['multiple']['active'] == true) { //multiple server //check if server_switch if (file_exists(getcwd().'/inc/server.xml')) { $server_switch_file = true; $xml = simplexml_load_file('inc/server.xml'); $server = $xml->server; //$config=$config['dbX'](w/r)['hostname'] $connections = $config; $connections['db'] = $connections["$server"]; //$connections=$connections['db'](w/r)['hostname'] $multiple = false; } //no server switch proceed... else{ //$config=$config['db']['hostname'] $connections = $config; $multiple = true; //$connections['dbX'](w/r)['hostname'] } } //single else{ $multiple = false; $connections = $config; //$config=$config['db'](w/r)['hostname'] } $separated=false; // No write server was specified (simple connection or just multiple servers) - mirror write link //write/read not supported yet (only read[write=read]) $this->db_encoding = $config['db']['encoding']; // //Make multiple false; by varify correct link $index=0; if($multiple == true){ if($separated){ } foreach($connections as $single_connection){ $_link = @mysqli_connect($single_connection['hostname'], $single_connection['username'], $single_connection['password'], $single_connection['db']); // Successful //Define correct connection/set multple false/write servers.xml if($_link) { $connections['db']=$single_connection; $multiple = false; $file = fopen("server.xml","w"); fwrite($file,"<serverswitch> <server>$index</server> <api>7</api> <version>alpha</version> </serverswitch>"); fclose($file); } $index++; } }
No comments:
Post a Comment