dbUtils is a simple, pure-PHP solution which allows anyone to manage multiple mySQL databases and tables. dbUtils currently consists of 3 functions to allow you to copy databases and tables (matching your regular expression) from one server to another, another to rename a database, and one to copy a table from one database to another. Designed originally for database backup without the need for command-line access or telnet access, but expanded to provide functions normally not available in most database management front-ends like phpMyAdmin.
By : gr8gonzo
<? // ### dbUtils for mySQL v1.0b ### // by Jonathan Hilgeman // // dbRename: // Description: Renames a specific Database // // Syntax: dbRename("Server_Name","Original_Database_Name","New_Database_Name"); // // Example: dbRename("ServerOne","MyyDatabase","MyDatabase"); // (Correcting the misspelling of "My" in the Database Name) // ----------------------------------------------------------------------------- // // dbCopy: // Description: Duplicates Select Tables or Databases from One Server to Another Using RegExps // // Syntax: dbCopy("From_Server","To_Server","Database_Regular_Expression","Table_Regular_Expression"); // // Example: dbCopy("ServerOne","ServerTwo","/.*/","/.*/"); // (Copies all databases and tables from ServerOne to ServerTwo) // // ----------------------------------------------------------------------------- // // dbTableCopy: // Description: Copies a Table from One Database to Another On the Same Server // // Syntax: dbTableCopy("Server_Name","From_Database","To_Database","Table_To_Copy"); // // Example: dbTableCopy("ServerOne","MyDatabase","YourDatabase","Shared_Info"); //
// Define Hosts (as many as you want) // Example: // $db["Server_Name"]["user"] = "mySQL Username"; // $db["Server_Name"]["pass"] = "mySQL Password"; // $db["Server_Name"]["host"] = "mySQL Host/IP Address"; // $db["Server_Name"]["port"] = "mySQL Port (usually 3306)"; // $db["Server_Name"]["time"] = "mySQL Connect Timeout in Seconds";
$db["ServerOne"]["user"] = "root"; // mySQL Username $db["ServerOne"]["pass"] = "password"; // mySQL Password $db["ServerOne"]["host"] = "localhost"; // mySQL Host/IP Address $db["ServerOne"]["port"] = 3306; // mySQL Port (default is 3306) $db["ServerOne"]["time"] = 10; // mySQL Connect Timeout in Seconds
// Send All Queries to Receiving Server foreach($SendQuery as $Query) { $dbResult = mysql_db_query($ToDatabaseName,$Query,$dbLink) or die(mysql_error() . " - Line 97 - $Query"); }
foreach($SendQuery as $DataBase => $QueryArray) { foreach($QueryArray as $Query) { if(substr($Query,0,15) == "CREATE DATABASE") { $dbResult = mysql_query($Query,$dbLinkTwo) or die(mysql_error() . " - Line 184 - $Query"); } else { $dbResult = mysql_db_query($DataBase,$Query,$dbLinkTwo) or die(mysql_error() . " - Line 188 - $Query"); } } }
// Success! return 1; }
// ____________ // |¯¯¯¯¯¯¯¯¯¯¯¯¯¯| // | Subfunctions | // |______________| // ¯¯¯¯¯¯¯¯¯¯¯¯ function dbConnect($Host) { // Bring in db Array global $db;
// Connect to Databases if($SocketTest = fsockopen($db[$Host]["host"], $db[$Host]["port"], &$errno, &$errstr, $db[$Host]["time"])) { // Server is Responding!
// Close Socket fclose($SocketTest);
// Try to Connect if($dbLink = mysql_connect($db[$Host]["host"], $db[$Host]["user"], $db[$Host]["pass"])) { // Connection is now active. return $dbLink; } else { // No connection. die("Server $Host is responding, but refused your connection."); } } else { // Server is Down! die("Server $Host does not appear to be running mySQL on port " . $db[$Host]["port"] . " or is currently down."); } }
// The Following Function Is Property of the People // Who Made phpMyAdmin (www.phpwizard.net) // :: Slightly modified and Cleaned Up by JH
// Get rid of repeated parentheses and misplaced commas $Definition = str_replace(",,",",",$Definition); $Definition = str_replace(",)",")",$Definition);
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.