Skip to content

Is it possible to repair MySql Databases with PHP?

by Rokan on August 3rd, 2009

I am attempting to devise a method of storing the structure of a MySql database and it’s individual tables in PHP. So far, it looks a bit like this:

public static $STRUCTURE = Array(
	'members' => Array(
		Array(
			'field' => 'id',
			'type' => 'int(11)',
			'null' => 'not null',
			'extra' => 'auto_increment',
			'primary key' => ', PRIMARY KEY(id)',
			'suffix' => ','
		),
		Array(
			'field' => 'member_name',
			'type' => 'varchar(32)',
			'null' => 'not null',
			'suffix' => ','
		),
		Array(
			'field' => 'member_pass_hash',
			'type' => 'varchar(32)',
			'null' => 'not null'
		)
	)
);



This may look complicated and overkill – however, this could easily be loaded from an ini file, another php file, and updated from the mysql database itself (with the latter being the easiest option). However, updating the structure from the database can obviously only be done when the database is functioning perfectly. This way, the array self::$STRUCTURE can be used to repair the database and to create queries. For example, the query:
CREATE TABLE members( id INT NOT NULL AUTO_INCREMENT , PRIMARY KEY(id) );

This query could easily be generated by using the following structure:

private static $STRUCTURE = Array(
	'members' => Array(
		Array(
			'field' => 'id',
			'type' => 'int(11)',
			'null' => 'not null',
			'extra' => 'auto_increment',
			'primary key' => ', PRIMARY KEY(id)'
		)
	)
);

A function could be devised to transform this table structure into a mysql query. Something like this:

static public function GetCreateQuery($table) {
	$structure = self::GetTableStructure($table);
	if($structure != 0) {
		$query = "CREATE TABLE " . $table . "( ";
		foreach($structure as $index) {
			$query .= implode(' ', $index) . " ";
		}
		$query .= ");";
		return $query;
	} else {
		return false;
	}
}

This function should return a valid MySql query if set up properly. If you run the query in your MySql panel, it creates the correct table with primary key and extra values. It should be easy from here to design a system that can troubleshoot its own MySql tables. For example, to repair a table, a query like this could be used (but obviously altered):
CREATE TABLE new_members_table(id int(11) not null auto_increment);
INSERT INTO new_members_table SELECT * FROM old_members_table;
DROP TABLE old_members_table;
RENAME TABLE new_members_table TO old_members_table;

Notice that the structure of the table created is the same as was created by our ‘GetCreateQuery’ function, and was structured by the self::$STRUCTURE['members'] array.

Using this could improve your PHP blogging system, forum software, commenting software.. any kind of web software which uses PHP and MySql databases – so there is no limit to its use!

From → PHP Tutorials

No comments yet

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS