How to export database with Adminer? - php

It's the first time i use ADMINER.
I want to export the database and i'm not sure to set correctly the parameters.
The database is in production and i don't want to make any mistakes.
See screenshot :
What are the correct parameters for :
1) Database : Use, drop + create, Create, Create + alter
2) Table : drop + create, Create, Create + alter
3) Datas : Truncate + insert, insert, insert+ update

Per your comment on or original question:
I want to export the database and import it in phpMyAdmin in my local environment to test and modify my client's website.
You want to recreate the database and data in a new environment and you are exporting SQL. Therefore, you will want to create tables where none exist, or discard and overwrite data if it does exist.
To accomplish this, you want to select the following options:
Database: Drop + Create - this will cause DROP statements to appear in the exported SQL before CREATE statements. This means that any existing databases with the same name will be dropped and all tables discarded. This is what you want to do if you want a clean test environment that matches production.
Tables: Drop + Create - for the same reason as above
Data: Insert - this will insert all data from your production database into your test copy database.

Related

Is there any way to determine whether an update to a database row was performed by a web application (php process), or run by hand through a DB GUI

I need to allow edits made via a DB GUI though to my auditing system so that they are also being tracked. I am updating the audit table using a BEFORE trigger, and i would like to be able to alter the audit row to indicate what the source of the update was (via an existing 'modified_by' column).
I cannot add an additional column to the audit table for this because I want to be able to copy from the updated table to the audit table without specifying table columns like below (I don't want this trigger to require that I update it with changes to the schema).
MY TRIGGER:
BEGIN
INSERT INTO contacts_audit SELECT * FROM contacts WHERE id = NEW.id;
END
PSEUDO TRIGGER:
BEGIN
IF(GUI EDIT) THEN
SET NEW.modified_by_id = 1; /* 1 is the administrator id */
END IF;
INSERT INTO contacts_audit SELECT * FROM contacts WHERE id = NEW.id;
END
A "DB GUI" connecting to the database is a client, a php connection is a client too, the same as any other way to connect to the database, so there is no flag like "this is php".
You can however use the USER() or SESSION_USER()-function in the trigger to get the active user/hostname of the current connection, so just use a different user/host for php and the gui if you want to seperate them.

Is there a way to copy a table structure to all tables in MySQL database at once?

phpmyadmin allows you to create a new table from a current table by copying its strucutre/strucute+data/data only...etc through a window similar to this...
However is it possible to copy the structure of a table to multiple tables in your database at once ?
one option would be to export the structure of the table. Then copy the sql generated by phpadmin. You could create a php script that iterates over the table names and runs the same alter/create query but replaces the table name.
You can do this using the show TABLES; query in mysql to get the tables then run alter $row[0] ... or create $row[0] ... queries from the export to do the work for you. just replace the table name with the varable name you have set in your loop.

Query entire DB for certain value

I am creating a php application that is the front end for a database.
Database Server Type: MySQL version 5.5.32
Web Server Type: Apache/2.4.4
OpenSSL: 0.9.8y
PHP 5.4.16
When populating the DB any blank fields that would need to be addressed in the future were entered as "TBD". I would like to create a query that searches the entire DB for fields that are equal to "TBD". If possible I would like the output of the query to have the following information:
Table Name:
Field Name:
Primary Key:
The PKs for all of my tables are named with the following convention: table name + ID. For example if a table is named "client" it's primary key field is called "clientID". I have set up a version of this report using arrays of the table names and fields name to generate multiple sql queries but I have to believe that there is a SQL wiz out there who can get this done in if not 1, just a query per table? This would be extremely beneficial because it would not require me to update the report, every time I make a change to the DB. Thanks!
You can use a script called anywhereindb
Download it here:
https://code.google.com/p/anywhereindb/
You simply have to upload a php file to your server, add the DB credentials and it'll search for a string throughout the DB. It has been very helpful.
Remember to delete the file after using it if is publicly accesible.
Try constructing your queries like this:
SELECT *
FROM tbl,
tbl1,
tbl2...tblx
WHERE tabl.condition OR ...

Check for changes in database schema and update

At our company we have a business solution which includes CMS, CRM and several other systems.
These are installed in several domains, for each of our clients.
The systems are still in development, so new tables and fields are added to the database.
Each time we want to release a new version to our clients, i have to go through their database and insert the new fields and tables manually.
Is there a way that this could be done automatically(a script maybe that detects the new fields and tables and inserts them?)
We are using php and mysql.
We would like to avoid backing up the clients data, dropping the database tables, running the sql query to insert all the database tables(including the new ones) and then re-inserting the customers data. Is this possible?
Toad for MySQL
DB Extract, Compare-and-Search Utility — Lets you compare two MySQL databases, view the differences, and create the script to update the target.
What you are looking for is
ALTER TABLE 'xyz' ADD 'new_colum' INT(10) DEFAULT '0' NOT NULL;
or if you want to get rid of a colum
ALTER TABLE 'xyz' DROP 'new_colum';
Put all table edits into an update.php file and the either call and delete it once manually or try to select "new_colum" once and update the database when it's not present.
OR what I do: "I have a settingsfield "software version" and use this as a trigger to update my tables.
But since you have to install the new scripts anyways you can just call it manually.

Create Table Else Alter Table

This seems to be a simple problem, but after a while of searching I can't figure out the answer.
I currently have a MySQL table in my local database used by a webapp, and them same table on a database in a remote server. Right now, I'm using the CREATE TABLE IF NOT EXISTS command through PHP to create the table on the databases:
CREATE TABLE IF NOT EXISTS users (
`id` int(10) NOT NULL AUTO_INCREMENT,
`username` varchar(18) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;
However, let's say I make a modification to the local database, adding a collumn, for example. It would be really annoying to have to go and change the remote database every time I change the local one. Is there an easier way to run code to create a table if it doesn't exist, and if it does exist, make sure it's structure matches that of the create table structure?
Here's an example, to make what I'm trying to convey a little clearer. Let's say on the local database I have a users table, and I decide that in my webapp I want to have another collumn, password. So I go to the local database and add a password collumn. Is there PHP/MySQL code I can run to check if the users table exists, and if it does, make sure it has a password collumn, and if not, add it?
What you are actually looking for are Migrations, e.g. you are looking for a Schema Management Tool that lets you manage your Database structure in versioned code diffs.
For instance, for your described scenario you would first create a script to create the table, e.g. 001_create_user_table.sql. Then you'd use the schema manager to connect and deploy these changes to your databases.
When you want to change or add something, you just write another script, for instance, 002_Add_Password_Column_To_User_Table.sql. Fill in just the code to do that change. Then run the schema manager again.
Typically, you tell the Schema Manager to go through all existing migrations files. On each run, the Schema manager will update a changelog table in the database, so when you run it, it will know which of your scripts it should apply.
The good thing is, you can add these migrations to your regular VCS, so you will always know which database schema you had at which version of your application. And you will have a proper changelog for them.
To directly answer your question you can create temporary procedures to detect field existence like using a query like this:
SHOW COLUMNS FROM table_name LIKE 'column_name';
However in the real world, database changes are general rolled into three scripts. A create script and two deltas one up and one down. Then the database is versioned so that you know at what state the database is in at any given time.
To specifically check for a password column you can use DESCRIBE:
$colExists = false;
$res = mysql_query('DESCRIBE `users`');
while ($row = mysql_fetch_assoc($res)) {
if ($row['Field'] == 'password') {
$colExists = true;
break;
}
}
if (!$colExists) {
// create column
}
However, you should check into replication or some other automated tool to see if they would be a better solution for you.
Follow these steps (you can easily implement this in PHP, I assumed that the name of the table is Foo)
1.) Run the following code:
desc Foo
2.) Based on the result of the first step you can make your create table command (and you should)
3.) Store your data from the existing table which will be replaced in a variable (Optional, you only need this if you can potentially use data from the old table)
4.) Modify the extracted rows from step 3.) so they will be compatible with your new definition (Optional, you only need this if you can potentially use data from the old table)
5.) Get the rows from your new Foo table
6.) Merge the results got in steps 4.) an 5.) (Optional, you only need this if you can potentially use data from the old table)
7.) Run a drop table for the old table
8.) Generate a replace into command to insert all your rows into the newly created Foo table (you can read more about this here)
After these steps, as a result, you will have the new version of the table. If your tables are too large, you can do a CREATE TABLE IF NOT EXISTS command and if that was not successful, run the alter command.
Also, you can make a library to do these steps and will use that in the future instead of solving the same problem several times.
EDIT:
You can connect the database using this function: mysql-connect (documentation here)
You can run a query using this function: mysql-query (documentation here)
Based on the first step you will get the field names (let's assume you store it in a variable called $bar) and you can use your result to generate your select command (connecting to the database where you have important data. It may be both):
$field_list = "1";
foreach ($bar as $key => $value)
$field_list.= ",".$bar[$key];
mysql_connect(/*connection data*/);
mysql_query("select ".$field_list." from Foo");
You can use your new resource to build up an insert command to insert all your important data after deletion recreation (about resources read more here, about how you can generate your insert you can read here, but I suggest that you should use replace into instead of insert which works like the insert, except that it replaces the row if it already exists, it's better here than an insert, read more here)
So, use mysql_connect and mysql_query, and the resource returned by the mysql_query function can be used for replace into later (I've linked now the URL's for everything you need, so I'm pretty sure you'll solve the problem.), apologies for being not specific enough before.

Categories