The server I'm using does not allow me to use LOAD DATA INFILE or even save a file as a .txt file. I have a .csv file that is just one column, with a name in each row. I want to insert this data into the name column of a table named people with name as one column and location as the other… where the locations will be updated at a later time.
It's a weird way to do this. But I need to do it this way and not using those previous commands.
Does anybody have any ideas? This has been giving me a problem for many hours. I can't figure out how to load the csv into my table column without using those previously mentioned methods that I can't use on my server.
Based on your issue and lack of general permissions you will have to do the following:
Replace the DOS carriage returns with unix new lines:
$contents=preg_replace('/(\r\n|\r|\n)/s',"\n",$contents);
Save the contents to the file, and then loop through each line, building an INSERT command that you execute to MySQL.
Related
I have a .dat file that contains a bunch of text information. What I need to do via php is be able to upload the .dat file to the server which I know how to do already. However once it is uploaded to the server I am not sure how in PHP to open the file and get what ever value is at the 180th position in the file.
The files should all be one row although even if its not I still just need the value from the first row 180th position and take that value and insert it into a mysql database table.
I am looking for any tutorial or documentation on how to do this
Thanks all
I Have created a table on my db, and filled all the records, using CSV file.
I need to do this weekly to keep the table updated.
I want to upload the new records without disturbing the old one onto the same table using csv.
[I have to pick the data from remote host and upload it locally on my server, i dont have access to the remote db]
Kindly guide me.
You can upload records from a CSV into a table VERY quickly using the load data infile syntax (http://dev.mysql.com/doc/refman/5.1/en/load-data.html)
The syntax is pretty simple, but also flexible. This is an example:
LOAD DATA INFILE 'data.txt' INTO TABLE table2
FIELDS TERMINATED BY '\t';
You can kick these off from a console or via code.
This will append to the table, not replace it, so if you don't truncate it first, it should work a charm.
You can of course also load the data manually by parsing the CSV file in your code and manually creating an insert statement for each line of code, but if the format is fixed already, this will be quicker and more efficient.
Edit: It appends the data. By default, no database will delete data from a table unless you specifically tell it to. Any insert statement is what you consider an append statement.
I am trying to import write a script that imports a csv file and parses it into mysql, then imports it in the db, I came across this article,but it seems to be written for ms sql, does anyone know of a tutorial for doing this in mysql, or better still a libary or script that can do it ?.
Thanks :-)
http://blog.sqlauthority.com/2008/02/06/sql-server-import-csv-file-into-sql-server-using-bulk-insert-load-comma-delimited-file-into-sql-server/
Using the LOAD DATA INFILE SQL statement
Example :
LOAD DATA LOCAL INFILE '/importfile.csv'
INTO TABLE test_table
FIELDS TERMINATED BY ','
LINES TERMINATED BY '\n'
(field1, filed2, field3);
If you are looking for script / library. I want to refer below link. here you can find :
PHP script to import csv data into mysql
This is a simple script that will allow you to import csv data into your database. This comes handy because you can simply edit the appropriate fields, upload it along with the csv file and call it from the web and it will do the rest.
It allows you to specify the delimiter in this csv file, whether it is a coma, a tab etc. It also allows you to chose the line separator, allows you to save the output to a file (known as a data sql dump).
It also permits you to include an empty field at the beginning of each row, which is usually an auto increment integer primary key.
This script is useful mainly if you don’t have phpmyadmin, or you don’t want the hassle of logging in and prefer a few clicks solution, or you simply are a command prompt guy.
Just make sure the table is already created before trying to dump the data.
Kindly post your comments if you got any bug report.
I've got a custom table in WordPress and I'd like to be able to export the data in that table as a FILE in CSV (semi-colon seperated) format.
I've got the data coming out properly, but how do I then save it as an attachment?
Keep in mind that when using WordPress, headers have already been set... and I really don't know how to get around that.
I'm not sure exactly HOW you're doing what you're TRYING to do ...
... but this link gives you a simple, straightforward way to write the data as a .csv file you can do a "save as" from your browser:
http://wordpress.org/support/topic/export-wordpress-db-table-to-excel
For these plugins, they use database to store row & column details. You have to extract the data in MySQL database tables ( those table names are usually named after the plugin's name ) .
If you are still not sure how to do, you can just copy the table being displayed in page in browser, then paste in Excel. Last, save as CSV or XLS if you like.
you can use this code. just copy and past and you have it :)
https://gist.github.com/umairidrees/8952054#file-php-save-db-table-as-csv
I have a requirement in which, i need to upload an excel file into SQL Server database. It is working well till this point.
But sometimes i am getting excel file in such a way, that the first 2 rows and columns are empty rows. That is not even fixed every time. so the format of the table after upload is not as expected. It is assigning F1, F2 as column names for the table in SQL Server.
It varies from file to file. I want to program it in such away so that the user can enter the row number and the column number from where the actual data is starting. So that while upload it should from line 3 and column 2.
I don't know how to specify that row and column while uploading.
Please help me to solve the same.
You could use http://phpexcel.codeplex.com/
You can use this to check if and where the data you expect is located in the excel.
If not you could try to find the first column/row with data and work from there.
First of all you can ask your user before starting upload (in form with input type="file") and after user uploads file and you show him an parsed excel data (by your script)..
To more concrete answer you should at least say, by what do you parse excel files and what excel format do you use...