I hope this is not a dumb question but I really can't find anything on the net as well as anything in the export settings in phpmyadmin.
My question: Is it possible to export data in phpmyadmin based on matching data to column names. For instance if you export a database or table sql statements are written like
INSERT INTO table VALUES ('1','2','3')
What I want to know if it is possible to export the data so the insert statements read
INSERT INTO table (number1,number2,number3) VALUES ('1','2','3')
The idea is that as I am writing a database I want to be able to export at certain times. But since I am constantly adding new fields in the tables I want to be able to import an old export and still be able to match the columns to the data. That way existing fields contain the data and new ones are just left empty.
Is this possible in just php?
Yes, it it possible. You should use the INSERT format that is similar to update:
INSERT INTO table SET number1 = '1', number2 = '2', number3 = '3';
Phpmyadmin knows how to do this, just search for "Update style inserts"
In "View dump (schema) of table"
Check the "Complete inserts" Option
you will get this
INSERT INTO table (number1,number2,number3) VALUES ('1','2','3')
Related
I have imported a SQL database from an Excel sheet, so it's a little bit messy. For example there's a product field with VARCHAR values such as product8. I would like to grep through these data using some regex, capture the id in this example, and alter column data types. As of now I would start preg_matching the long and hard PHP way, and I'm curious how a database normalization is done right using SQL commands. Thanks for your support in advance.
you can select case, to pull the ids
select right(product,length(product)-7) as productID from table
this will pull the numbers, then you can do whatever
working on the PHP project related to web scraping and my aim is to store the data into the mysql database,i'm using unique key index on 3 indexes in 9 columns table and records are more than 5k.
should i check for unique data at program level like putting values in arrays and then comparing before inserting into database ?
is there any way so that i can speed up my database insertion ?
Never ever create a duplicate table this is a anti SQL pattern and it makes it more difficult to work with your data.
Maybe PDO and prepared statement will give you a little boost but dont expect wonders from it.
multible INSERT IGNORE may also give you a little boost but dont expect wonders from it.
You should generate a multiinsert query like so
INSERT INTO database.table (columns) VALUES (values),(values),(values)
Keep in mind to keep under the max packet size that mysql will have.
this way the index file have to be updated once.
You could create a duplicate of the table that you currently have except no indices on any field. Store the data in this table.
Then use events to move the data from the temp table into the main table. Once the data is moved to the main table then delete if from the temp table.
you can follow your updates with triger. You should do update table and you have to right trigger for this table.
use PDO, mysqli_* function, to increase insertion into database
You could use "INSERT IGNORE" in your query. That way the record will not be inserted if any unique constraints are violated.
Example:
INSERT IGNORE INTO table_name SET name = 'foo', value = 'bar', id = 12345;
I have some questions about customizing export result(excel) with php. The idea is i want to export my query of my raw data (mysql table) to excel file but with some customization in the result.
for example i want to have result which is summary of the table like below table:
The 3rd column until 7th column is named based on the last 5 days of my report date.
My idea is:
1. create temporary table using format as the result table i want to generate
2. Insert the table with my raw data.
3. Delete those table.
Is that efective?or is there any better idea?
You can always use a view. Which is essentially a select statement with your data in there, and which will be updated whenever your tables are updated. Then you can just do a 'select * from view_name' and export that into your excel.
Depending on the size of the data, there is no need to think about performance.
Edit the data before
You can have a temp table. Depending on the data, this is very fast if you can select and insert the data based on indexes. Then you make a SELECT * from tmp_table; and you have all your data
Edit the data after
You can just join over the different tables, get the data and then loop (read as foreach) over the result array and change the data and export it afterwards
I need on coding to check a mysql DB to see if it needs to be updated. The DB is for stock info & will only download the info if it is not currently in the DB. I'm trying to figure how to design my code without making some long peice of coding that may be able to be done in a few lines of code.
I currently am using the information_schema UPDATE_TIME info for the table, so I have that info. But where I'm stuck is how to I use php to check if the DB needs to be updated.
So in essence, I'm looking for a code snippet that will check to see if the table needs to be updated but will check for the duplicate info (i.e. if it makes it through an "if" statement & then downloads it anyway, if it is duplicate info, it will not insert it [I can use the date for this]).
Thanks
Think of this :
$result = mysql_query("SELECT * FROM tableName" WHERE ConditionYouHave)
or die(mysql_query("INSERT INTO tableName VALUES ('value1','value2','value3')"));
Do you have some sort of datafeed you can run through to check for new data? If this is the case I would go line-by-line through the file looking for new rows. When you find one, you insert it into your table. If the row already exists just continue on to the next row. This can be done fairly simply in PHP.
INSERT INTO table_name (id, column2)
SELECT {$int_id}, '{$str_column2}'
FROM table_name
WHERE id = {$int_id} AND column2 != 'what ever you want'
ON DUPLICATE KEY UPDATE column2 = VALUES(column2)
table_name is your own table's name.
I assumed you have a id column. And column2 is just an example.
You can validate your row through the WHERE and have whatever condition you wish.
Your question's comment links to this example.
I was wondering how can I select a value from a database that a user just entered it into and then add it to another mysql table all in the same script before the script is finished running.
You're probably looking for an insert ... select statement.
If you're talking about adding a value that a user just entered into a form, to something, and then putting that into the database, you should do the addition while in PHP. There's no point in going to the database after you've just inserted the value for this purpose.
If I'm misunderstanding something, please elaborate your question and let us know WHY you would want to figure out a just-inserted database value and do an operation on it, rather than trying to do it before you insert in the first place.
Also, if it's a fairly simple modification consider using an UPDATE statement, not a select --> insert.
Like nash said, you perform a select.
But to get the data from the row that the user just entered, you'll need:
mysql_insert_id()
Which grabs the last ID inserted (this is assuming you have an increment id column)
So assuming just entered his first and last name in a form, you'd insert his first and last name in the database(which i assume you know how since the title of this question is "SELECT a value from MySQL database"), you can get what he just entered by:
$last_id = mysql_insert_id();
If there are no rows on that table yet, then this will return 1. $last_id is now 1 (one).
To select:
SELECT * FROM users WHERE userID = "$last_id"
this will grab what the user just inserted....however, this seems pointless as you can use the variables from the form he just filled
enter code here
In the PHP MySQL module, you normally perform a mysql_select_db() to switch database.
You can insert your data into tables in different databases by switching between them with that function.
However, you can insert data into any table of any database (which the user has access to) by prefixing the database name to the table like so:
INSERT INTO test_db.test_table (`column1`,`column2`) VALUES ('abc',123);
You can use that also to insert data from one table into another using:
INSERT INTO `db1`.`myTable` (`column1`,`column2`) SELECT `column1`,`column2` FROM `db2`.`myTable` WHERE `id`= 5
The WHERE id part should obviously match the id of a row in db2.myTable
If you use doctrine you have the inserted data in the object representing the table and in addition you have primary key assigned for the record inside the object.
Con is doctrine is huge database abstraction layer, so if your application is not big doctrine is hammer for mosquito.
what is the structure of your database? The names of your tables, columns?
Some tutorial that you may want to look at: (grabbed from google)
http://www.phpf1.com/tutorial/php-mysql-tutorial.html
In theory you perform a select, take the data you need and perform an insert.