Completely lost MySQL Database, only original PHP files remain

This post is similar to " How to generate a MySQL Database using only original PHP script files?" but it is locked, so i made this topic to clarify a couple of things.
I have completely lost my database, it has been many years since i had access to it, and any backups where destroyed as well, but the original php files remain.
user Noppy had a very interesting notion about this in order to reverse engineer using the php files, debugging the files and finding the tables in the errors, and then to recreate them (this is what i want to do but don’t know how)
How exactly can this be achieved, because i am not a programmer and using dreamweaver does not give me the desired solution. I only know that i can use navicat to create the tables and everything else needed after i find out what is missing.

You realize what you would be doing is reading through the php script and figuring out the names of the tables and the columns in those tables right? You would not retrieve any data that was lost.

Yes i know, but in my case the data is not important, i just want all tables to have null values

I don’t think that is what you want if that’s what you meant. I have seen quite a few table schemas and many have NOT NULL fields.

If it were me in your situation, I would place the files into an IDE project so I could find various database related Strings, use an advanced text editors find to locate the Strings, or write a recursive PHP script to locate the Strings.

In particular, I would look for script that made database connections, and the strings “CREATE TABLE” and “ALTER TABLE”

As it’s been many years since you last had access to it, there’s a high probability that the PHP scripts use functions that have either been deprecated or removed from PHP. Were the PHP scripts custom written or “off the shelf”?

I might be misunderstanding your problem in which case please disregard this, and it’s not a solution for your current problem but a prevention for the future: always have as part of your application php install scripts that define your database tables and fields and create them for you so that if you ever run into this problem again you can just run the install script

1 Like

This was a custom written program for me. And as far as i understand coding, the tables only held user data values, such as name, surname, time spent on a workstation etc., which at this point i don’t need. I just need a database that works with the tables and its’ columns and the type of data for every column.
I will try and use an IDE project and see if i could determine the database related strings from there. Thank you all for your input.

How I have things setup in order to deal with these kinds of problems is something along the lines of the code below. It took me two days to write but I think it was well worth it, because it can give me a blank or updated copy of the database with all its tables and fields and data types:

<?php 

Class Users extends DatabaseObject 
{
	protected $tableName = "users";
	
	protected $database;
	
	protected $fields = array(
		"id" => array(
			"type" => "int",
			"primary" => 1,
			"autoincrement" => 1,
		),
		"userName" => array(
			"maxLength" => 20,
			"null" => 1,
			"type" => "varchar"
		),
		"firstName" => array(
			"maxLength" => 20
		),
		"lastName" => array(
			"maxLength" => 20,
			"null" => 1
		),
		"email" => array(
			"maxLength" => 40,
			"default" => 10
		),
		"lastLogin" => array(
			"type" => "int",
			"maxLength" => 10,
			"null" => 1
		)
	);
	
	protected function install() 
	{
		parent::install();
		//custom code here
	}
}

Class DatabaseObject 
{
	protected function install () 
	{
		$tableExists = 0;
		try {
			$tableExists = $this->database->query("SELECT 1 FROM `$this->tableName`", 0);
		} catch (Exception $e) {}
		$tableExists ? $this->updateTable() : $this->createTable();
	}
	
	protected function createTable () 
	{
		/* create and execute a mysql query like the following :
		CREATE TABLE IF NOT EXISTS `users`(
		 `id` int NOT NULL  AUTO_INCREMENT  PRIMARY KEY ,
		 `userName` varchar(20) ,
		 `firstName` varchar(20)  NOT NULL ,
		 `lastName` varchar(20) ,
		 `email` varchar(40)  NOT NULL  DEFAULT '10' ,
		 `lastLogin` int(10) 
		)*/
	}
	
	protected function updateTable () 
	{
		//  Generate and execute mysql that would just update the necessary fields if you change them in the fields array defined in the instance of this object, in this example the users class
	}
	
	protected function getFieldSql() 
	{
		$rv = "";
		foreach ($this->fields as $key => $value) {
			//code ...
		}
		return $rv;
	}
}

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.