Output Database records at data insertion automatically - php

I was wondering if there was a type of code where when data is inputted into the database, a text file would be updated automatically. I have a website with food menu items and want to output food items to a text file, drinks to another text file and dessert items to another text file. Is it possible ?
SELECT tblnum,food_name FROM clients
enter code hereINTO OUTFILE '/tmp/food_orders.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'

You should use RethinkDB to achieve this.
https://www.rethinkdb.com/
With RethinkDB you can push to a script that processes the given json.

Related

LOAD DATA INFILE with additional columns not working

Txt file:
942,Nike%27s+cities,2469,2,2,164
316,just+me,820,1,1,286
827,RES+4+GOLD,1523,1,1,214
1171,KnightBlood,1550,1,1,211
1172,athens,2095,2,2,177
The above file are alliances from a game i'm trying to make a tool for the format is the following:
id, name, points, towns, members, rank
In my database i'm storing 3 additional columns:
id, name, points, towns, members, rank, world_server, world_id, date
I've looked around and tried multiple different options/queries but I can't seem to get it to work. My current query, in PHP, is:
"LOAD DATA LOCAL INFILE /path/file.txt
INTO TABLE alliance
FIELDS TERMINATED BY ',' LINES TERMINATED BY '\n'
SET world_server = $world[server], world_id = $world[id], date = $curDate"
I get that this is probably a duplicate question but I have searched through stackoverflow, mysql docs, etc... for multiple days now and I don't get what's wrong with my query. I am not getting any error messages either which doesn't help, my database just stays empty.
Hope someone can help,
A desperate student.

Export large table using a SELECT query into client machine

I have a table with over a million rows. The PHP export script with headers and ajax that I normally use to create the user interface to export to csv, is not able to handle these many rows and times out. Hence looking for an alternative
After a fews days of digging I collated the below script from the internet, this downloads wonderfully but only to the local server > mysql folder
What I am looking for is to create a php mysql script so I can let users download large tables to csv's through the php user interface itself
SELECT 'a', 'b', 'c' UNION ALL SELECT a,b,c INTO OUTFILE '2026.csv' FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '"' LINES TERMINATED BY '\n' FROM table ;
You need to fetch from the DB with paginated results.
Select * from a LIMIT 0,100; the result of this query you must put in in a variable, then parse it like this:
Export to CSV via PHP
After you put the first 100 elements to the csv, you have to fetch from 100 to 200 and so on...then to reopen the csv and put the 100 to 200 elements and so on.
And then finally send the csv to the user.

Nested php loops used to parse CSV file input into DB - SQL insert statement fails at arbitrary cycle

I have an issue with a function I am writing whereby I take a large csv file and input it into an array 2d array.
The csv file an export from a database table with various records,
The following are the steps the function carries out:
//A. get temporary file path
//B. place file into array as a long string
//C. parse string into array based on rows (\r)
//D. parse row strings into arrays for columns (,)
//1. Query db for price codes
//1.1 Loop through CSV top row to find price code x axis position
//2. Loop through price codes SQL
//2.1 Loop through price codes column from CSV file
//2.1.1 IF match price code
//2.1.2 Check if id match on price list array
//2.1.3 Add record to prices table
//Success
//Failure (this could kill the loop maybe)
//3. Close loops and close sql connection
Price codes are codes relating to what geographical area the price is applicable too.
The function loops through all the price codes in the database
Within the price code loop a sub loops sifts through the csv array looking for records with a price code matching the current loop.
When a match is found, input into database
Here is the code:
Click for code
I output debugging text every step of the loop to see if everything executes successfully.
The loop executes correctly - however for some unknown reason after a certain point the mysqli query doesn’t add further records to the database.
How could this happen? even when it doesn’t give an error, and the SQL query string which is outputted to the page looks intact.
Consider running pure SQL by uploading all your data pieces, csv_array and onPriceList into MySQL temp tables (regularly cleaned out if they regularly change) and run one SQL query, avoiding nested foreach loops and if logic.
You can easily upload csv data into MySQL in one call using the LOAD DATA INFILE (or insert with PHP iterations):
LOAD DATA INFILE '/path/to/csv/file.csv'
INTO TABLE csvdata
FIELDS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\r\n';
Then, run one straightforward append query (adjust below to align to actual table and field names as screenshot does not show):
INSERT INTO prices (PriceCode, SellingPrice, StockCode)
SELECT c.priceCodePosition, c.pricePosition, c.model_idPosition
FROM csvdata c
INNER JOIN pricecodes p
ON c.model_idPosition = p.Code
WHERE c.model_idPosition IN (SELECT p.prices FROM onPriceList)
In case you need to capture the records that do not match logic (as you need with the echo statements), run the antithetical statement (which would be records that are not appended):
SELECT c.priceCodePosition, c.pricePosition, c.model_idPosition
FROM csvdata c
LEFT JOIN pricecodes p
ON c.model_idPosition = p.Code
WHERE c.model_idPosition IS NULL
OR c.model_idPosition NOT IN (SELECT p.prices FROM onPriceList)

MYSQL load data local Infile & special characters (& PHP)

I am trying to understand how to write a query to import my goodreads.com book list (csv) into my own database. I'm using PHP.
$query = "load data local infile 'uploads/filename.csv' into table books
RETURN 1 LINES
FIELDS TERMINATED BY ','
ENCLOSED BY '\"'
LINES TERMINATED BY '\n'
(Title, Author, ISBN)";
This results in data in the wrong columns, I also can't get the ISBN, no matter what I try, guessing the equals (=) sign is causing some problems.
Here's the first two lines of the CSV:
Book Id,Title,Author,Author l-f,Additional Authors,ISBN,ISBN13,My Rating,Average Rating,Publisher,Binding,Number of Pages,Year Published,Original Publication Year,Date Read,Date Added,Bookshelves,Bookshelves with positions,Exclusive Shelf,My Review,Spoiler,Private Notes,Read Count,Recommended For,Recommended By,Owned Copies,Original Purchase Date,Original Purchase Location,Condition,Condition Description,BCID
11487807,"Throne of the Crescent Moon (The Crescent Moon Kingdoms, #1)","Saladin Ahmed","Ahmed, Saladin","",="0756407117",="9780756407117",0,"3.87","DAW","Hardcover","288",2012,2012,,2012/02/19,"to-read","to-read (#51)","to-read",,"",,,,,0,,,,,
What I want to do is only import the Title, Author, and ISBN for all books to a table that has the following fields: id (auto), title, author, isbn. I want to exclude the first row as well because I dont want the column headers, but when I have tried that it fails every time.
What's wrong with my query?
Unfortunately, in case you want to use the native MySQL CSV parser, you will have to create a table that matches the column count of the CSV file. Simply drop the unnecessary columns when the import is finished.

PHP Imported CSV data cut short when encounterng a comma

I have a simple PHP script that loops over data in a CSV file, and adds the records to the database accordingly. One of my fields is a description field, but that description field itself has a comma (or multiple comma's) in it. It seems as though data for that field is only read up until the comma, however the next field is correct, so it is not as though the field after that is the remainder of the description, is is using the next column which is right.
Am I supposed to escape the comma? I am adding this data to a MySQL database, could that be where the issue is being caused?
My SQL query could be something like:
$description = $data[7]; //description column eg: "hello, my name is xxxxx, I am old"
INSERT INTO tblsomething (id, description) VALUES ($id, '$description');
The above statement only inserts the description as "hello" and nothing after the first comma it encounters.
Any ideas why this is?
Many thanks,
Simon
EDIT: This is solved, apologies to all as it was a silly mistake. It appears that the person who did the front end was creating arrays of content using the patter ',' to split the content. IT seems that the description - although supposed to be one array entry - was being split into multiple entries due to it containing comma's. This will be solved by using a more rare character like the pipe symbol to create our separators.
Thanks to all
Because it's not a CSV file. Fields in a CSV file that contain commas are supposed to be delimited by double quotes; this way the CSV functions in PHP will handle them properly.

Categories