Importing certain table columns into database table - php

I have a new database table called schools. I have an existing database table on another server that has similar school info that I need. Example: Name, City, State etc., I am using phpMyAdmin to manage this.
My question is... What is the best/detailed way to export the school data from the outside server's database table into the new database table but only extracting and imported the columns I need (example: Name, City, State, etc)?

In SQL, select the data you need,
SELECT (name, city, state) FROM schools
Then in PHP, output it to some format,
$results = $query->fetchAll();
for ($i=0; $i<sizeof($results); $i++) {
# format the output
}
phpMyAdmin has functions to import (and export) data in certain formats including CSV.
You can then import the CSV file to the new server.
You could also do it the other way: save the whole database and import it to the new server. Then delete the columns you dont need with SQL following this syntax,
ALTER TABLE tableName DROP columnName;

If you are using phpMyAdmin then it allows you to export a whole table in SQL format to a downloadable file. That would seem to be the best way to export the data. You can then upload this file to the destination server and import it via phpMyAdmin.
One caveat with this method is that you need to make sure your table names don't clash. If they do, you will need to rename one or the other (I'd suggest renaming the source table before you export it).
Once you've imported the whole table you can do an INSERT INTO...SELECT on the required fields.

Related

Load csv into MySQL, using a new table

I am using phpMyAdmin to load a CSV file into a MySQL table but, does the table have to be created before the load?
I have read that if the workbench is used ( Import a CSV file into MySQL workbench into a new table dynamically ) the latest version creates the table on the fly but, is this possible with phpMyAdmin.
Here is the code that doesn't work
load data local infile '/Users/...'
into table skin_cutaneous_melanoma
fields terminated by ','
lines terminated by '\n'
Error is:
#1146 - Table hospital.skin_cutaneous_melanoma' doesn't exist
Sure, phpMyAdmin can do this.
From the main page, look for the Import tab. This is, of course, how you expect to import any file including if you already have the database and table created, but if not phpMyAdmin creates a temporary database and table for you using generic names and best guesses as to the data types for your columns. Note that this is probably not the ideal structure, but is the best guess based on the existing data in your database. For best results, you'll probably want to put the desired column names at the top of your CSV file and select the corresponding option during import, or rename them after import (likewise with the database and table names) -- but it is absolutely possible to do the import to a new table.

Import data from csv field into a database field using a lookup table

I wish to import data from a csv spreadsheet into an empty database field named "parishname" that normally uses a lookup table to add data using the query -
SELECT "Parish"."parid","Parish"."parishname" FROM "Parish" ORDER BY 2.
Can someone give me the code required to amend the query to allow data in the csv field to bypass the query. I have little or no MYSQL knowledge and am using Appgini software to build the database.
SELECT Parish,parid,Parish,parishname FROM Parish ORDER BY 2.
if not work then tell me more and proper description of your question

Update MySQL Table using CSV file

my Current MySQL table employee_data has 13k rows with 17 columns. The data in the table came from a CSV file Employees.csv. after importing my csv data I added a new column 'password' (so its not in the csv file) Password is edited and accessed via a web portal. I now have an updated csv file and I want to update my main table with that data but I don't want to lose my password info.
Should I import my new CSV file into a temp table in my database and some how compare them? I am not sure where to start and I am open to recommendations.
I am now realizing I should have kept my password info in a separate table. Doh!
I guess I could created a php file that compares each row based on the employee_id field but with 13k rows I am afraid it would time out possibly.
I would do it like this :
Create a temp table using CREATE TABLE new_tbl LIKE orig_tbl; syntax
use LOAD DATA INFILE to import the data from the CSV into the table
Use UPDATE to update the primary table using a primary key / unique column (perhaps employee_id)
I have worked with tables containing 120 million lines and imported CSV files containing 30 million lines into it - this is the method I use all of the time - much more efficient than anything in PHP (and thats my server side language of choice)
Try other tools other than php based ones phpMyAdmin
MySQL workbench is a great tool,
based on you connection it will take a while to query the database with your data.
There are workarounds with php timeout limit,
set_time_limit();

modify csv with php before import / upload with php

I want to upload a csv to a database with php, but before i do that i want to modify some of the content.
The database table the csv will come from has 3 columns: id, postcode, debt_amount
The database table the csv will go to has 4 columns: id, postcode, debt_amount, count
What i want to do first is modify the full postcode to just show the first part before the 'space'.
Then i want to consolidate all the rows that have the same modified postcode, this will do two things:
Count the number of rows with the same modified postcode and place the total number into the consolidated row in the column 'count'.
Add up the 'debt_amount' column for the same modified postcode and put the total amount into the consolidated row under the 'debt_amount' column.
These processes would need to run together.
After that is done i want to upload it to the database.
I don't know if this is the best way of doing it, or if i should process the data from the first database first and export it into a CSV, to just allow me to upload the CSV on the other database.
Any help on either process would be good.
Thanks
I think it is best to process this data in MySQL itself. You may decide if you would like to process it in the source database or the target database.
So, if the processing involves:
modify the postcode
count #rows with same modified-postcode
sum debt_amount for same modified-postcode
Either do the processing in the source database, store the results in a temporary table, generate the CSV and then import to the target database. Or generate the CSV from the source DB, import to the target database in a temporary table, do the processing and store the results to the final table.
Do the standard file upload.
Read the CSV content from the temporary upload file.
Process the CSV Data (e.g. with SplFileObject­Docs.
Insert the processed data into your database.

Importing a specific column into phpmyadmin database

I have been given an excel document which contains information about different embassies, their contact numbers etc. Most of this information is in the database already, but I need to extract one column from this file (emails) and insert it into a specific column (emails) in the database.
Is this possible, or can you only import an exact copy of what you want in the database?
Thanks for any help
Export the table from phpmyadmin as CSV for excel -
drag it into excel -
add the desired column -
Save -
export the table again from phpmyadmin as .sql -
copy the table creation(Not the data) -
delete the table from phpmyadmin-
Run the table creation query so you now have a blank table -
Import the excel csv.
IMPORTANT: backup your database
You may want to make a script with php-excel-reader which goes throught every row in the file, and updates the corresponding database row. I don't think that you can import a file "selectively" in phpMyAdmin.

Categories