PHP MySQL OUTFILE command - php

If I use the following in a mysql_query command:
SELECT *
FROM mytable
INTO OUTFILE '/tmp/mytable.csv'
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n';
Where is the tmp file relative to, to the MySQL database somehow or to the PHP file?
If it does not exist will it be created?
If I would like it to appear 1 folder up from the PHP file which does it, how would I do that?

According to The Documentation On Select, it's stored on the server and not on the client:
The SELECT ... INTO OUTFILE 'file_name' form of SELECT writes the selected rows to a file. The file is created on the server host, so you must have the FILE privilege to use this syntax. file_name cannot be an existing file, which among other things prevents files such as /etc/passwd and database tables from being destroyed. As of MySQL 5.0.19, the character_set_filesystem system variable controls the interpretation of the file name.
And, more to the point:
The SELECT ... INTO OUTFILE statement is intended primarily to let you very quickly dump a table to a text file on the server machine. If you want to create the resulting file on some other host than the server host, you normally cannot use SELECT ... INTO OUTFILE since there is no way to write a path to the file relative to the server host's file system.
So, don't use it in production to generate CSV files. Instead, build the CSV in PHP using fputcsv:
$result = $mysqli->query($sql);
if (!$result) {
//SQL Error
}
$f = fopen('mycsv.csv', 'w');
if (!$f) {
// Could not open file!
}
while ($row = $result->fetch_assoc()) {
fputcsv($f, $row);
}
fclose($f);

Where is the tmp file relative to?
A: The file will have the result of the select * from mytable
If it does not exist will it be created?
A: yes
If I would like it to appear 1 folder up from the php file which does it, how would I do that?
A: if you want one folder up from the fileYouAreRunning.php then make path like that: "../mytable.csv"

Your current query has an absolute path. So the outfile will not be relative to anything, but saved to /tmp/mytable.csv.
I'd say, the safest bet would be to keep useing absolute paths, so check in your php what your absolute path to the parent is, and then add this to your query using a variable.

Related

file not found during import csv to mysql db table

the folders structure of my site is the following: www.mysite.it/site/scripts.
At this path there are two files: import.php and tabella.csv.
tabella.csv is a CSV file like this:
"2016-09-02", 100.01, 4005.09, 5000, 1.09, 120.09, 100.5, 200.77
"2016-09-03", 150.01, 4205.09, 5600, 1.10, 150.09, 300.5,300.77
import.php is the PHP script to execute te import and it's a file like this:
<?php
$csvFile = "../scripts/tabella.csv";
$db = #mysql_connect('****', '****', '****');
#mysql_select_db('******');
$query = 'LOAD DATA LOCAL INFILE \' '. $csvFile .' \' INTO TABLE rame
FIELDS TERMINATED BY \',\'
LINES TERMINATED BY \'\r\n\'
IGNORE 1 LINES
(
giorno,
lmedollton,
changedolleuro,
euroton,
lmesterton,
delnotiz,
girm,
sgm
)';
if(!mysql_query($query)){
die(mysql_error());
}
mysql_close($db);
?>
The error is 'file not found': I tried to use 'tabella.csv' and the the realpath also (using realpath PHP function) but the error is always the same. Which is the correct string that I have to assign to $csvFile variable? Can you help me, please?
You should provide an absolute path to your csv file e.g.
$csvFile = "/var/www/htdocs/site/scripts/tabella.csv"
because mysql will not be able to resolve the path relative to apache httpdoc-root directory. See also the Mysql-Doc:
If LOCAL is specified, the file is read by the client program on the
client host and sent to the server. The file can be given as a full
path name to specify its exact location. If given as a relative path
name, the name is interpreted relative to the directory in which the
client program was started.

Upload CSV file and import it to database using Laravel

I have this method that uploads a file of a CSV format, but now i want to know how to upload it into columns in my database.
My method:
public function postUpload ()
{
if (Input::hasFile('file')){
$file = Input::file('file');
$name = time() . '-' . $file->getClientOriginalName();
// Moves file to folder on server
$file->move(public_path() . '/uploads/CSV', $name);
return 'Ok'; // return for testing
}
}
So my question would be within this method how can i can put it into the database ?
this one should work for you, it uses PDO + mySql's "LOAD DATA" approach
private function _import_csv($path, $filename)
{
$csv = $path . $filename;
//ofcourse you have to modify that with proper table and field names
$query = sprintf("LOAD DATA local INFILE '%s' INTO TABLE your_table FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\"' LINES TERMINATED BY '\\n' IGNORE 0 LINES (`filed_one`, `field_two`, `field_three`)", addslashes($csv));
return DB::connection()->getpdo()->exec($query);
}
So combined with your code, it could be something like the below
public function postUpload ()
{
if (Input::hasFile('file')){
$file = Input::file('file');
$name = time() . '-' . $file->getClientOriginalName();
//check out the edit content on bottom of my answer for details on $storage
$storage = '/some/world/readible/dir';
$path = $storage . '/uploads/CSV';
// Moves file to folder on server
$file->move($path, $name);
// Import the moved file to DB and return OK if there were rows affected
return ( $this->_import_csv($path, $name) ? 'OK' : 'No rows affected' );
}
}
EDIT
One thing to be noted, as per the error you report in comments which is probably some permissions issue (OS error code 13: Permission denied)
Please see: http://dev.mysql.com/doc/refman/5.1/en/load-data.html
"For security reasons, when reading text files located on the server,
the files must either reside in the database directory or be readable
by all. Also, to use LOAD DATA INFILE on server files, you must have
the FILE privilege. See Section 5.7.3, “Privileges Provided by
MySQL”."
As reported on mySql bug tracker (http://bugs.mysql.com/bug.php?id=31670) it seems that you need particular permission for all the folders in the csv file path:
All parent directories of the infile need world-readable I think
aswell as just the directory and infile...
So for an infile here: /tmp/imports/site1/data.file
you would need (I think, 755 worked) r+x for 'other' on these
directories: /tmp /tmp/imports
as well as the main two: /tmp/imports/site1
/tmp/imports/site1/data.file
To sum up:
To solve the "sqlstate hy000 general error 13 can't get stat of..." issue you have to move the uploaded file to a location with proper permissions (so not neccessarily the current one you are using) try something like "/tmp/import".
While load data infile is the quickest way, I prefer to use a lib like https://github.com/ddeboer/data-import or https://github.com/goodby/csv for 2 reasons.
It is extensible, what if your data source changes to excel files or a mongo db or some other method?
It is mallable, if you need to convert dates, or strings or numbers you can do it conditionally which cannot be done with a batch command.
my 2c

php&mysql fopen cannot open the file

I am using PHP & MySQL to generate a dynamic web page. Now I want to make the search result into a file.
Firstly, I use
$query = "select * from database into outfile 'query.txt'";#mysql($query);
BUt it cannot work;
Then, I try to use the "fopen" function.
$fp=fopen("query.txt","w+") or exit("Unable to open file!");
if($result_specific){
while( $row = mysql_fetch_array( $result_specific,MYSQL_ASSOC )){
echo fwrite($fp,$row["p1"]."\t".$row["p2"]."\t".$row["p3"]."\n");
}
}
fclose($fp);
Unfortunately, it tells me "Unable to open file!".
Maybe it is a wrong Url?
But I don't know how to specify the correct URL.
select * from database into outfile 'query.txt'
You haven't specified a path - only a filename. The file will be written to the current working directory of the MySQL instance. It will be written with the uid of the the user running the instance. It's impossible to say from the information you've provided what permissions the resultant file will have - on a Linux/BSD/Posix system the permissions will be based on he the umask inherited by the DBMS instance.
$fp=fopen("query.txt","w+")
Is you PHP looking in the right directory? What are the permissions on the file?
or exit("Unable to open file!");
That 'or' will not do what you think it does - should be '||'
if($result_specific){
...
What has this got to do with the problem?
Go back and use full paths and check the permissions.

file_put_contents only outputs a single file?

Collating some data week by week I serialized an array of the appropriate data and output that to a temporary file using file_put_contents.
Inclusive of some output statements to try and establish what is happening the code looks like this:
echo "<br/> collate session data <br/>";
print_r($this->session_Data);
$serialized_Array = serialize($this->session_Data);
$file_Name = 'session_Data_' . $this->week_Number;
echo "file_Name: " . $file_Name;
file_put_contents($file_Name, $serialized_Array);
The file name definitely gets updated appropriately and there is definitely data to output.
But this will only ever output the first weeks data to the tmp file.
I get no permission errors I get status 200 OK but no files are created.
Am I missing something here??
You are writing the file in the same directory as the script. In a default configuration the webserver process has only read and execute rights in that directory.
create a separate directory for the files, and give the webserver process write permission to that folder.
give the webserver process write permission to the folder the script is in. Be aware that this can be a security issue.

SugarCRM save database data to file

Is there any way I can save the data of a specific table of the sugarcrm database into a doc file ?
I have a custom module with username,some notes and date. I want to write this into the database and into a file as well.
Its not just a php file. I want to use logic hooks and write the code. I want to use the logic hooks to access database and then write the data into the file.
Thanks in advance
Saving as a DOC file probably isn't the best idea, since it is primarily used for formatting information. A standard .txt file is usually what you would use for such a process.
With that said, there isn't any methods built into sugar that will let you do this. You will need to build the capability into the module.
What exactly are you attempting to accomplish? There is a very powerful auditing tool set, which is good for seeing revisions to a module object. If you are just wanting to monitor changes to the table, you can setup logging for that table/database inside of SQL.
+++Ok, if you are just looking to write to a file after saves, follow the instructions at: http://cheleguanaco.blogspot.com/2009/06/simple-sugarcrm-logic-hook-example.html for a quick how-to on getting the logic hooks working. You are going to want to make a php file that simply uses the data passed to it via the bean class, and either writes to the file directly from the data within bean, or uses the bean->id parameter to do a SQL query and write to the file from that data.
Also, is this a DOC file that is going to be immediately generated and then destroyed at the end of the transaction? Or is it more of a log file that will be persistent?
++++That is simple enough then
Where you have the Query right now, replace it with:
$file = fopen($pathAndNameOfFile, 'a+') or die('Could not open file.');
$query = "SELECT * FROM data_base.table";
$result = $bean->db->query($query,true);
$dbRowData = $bean->db->mysql_fetch_assoc($result);
$printedArray = print_r($dbRowData);
fwrite($file, $printedArray) or die('Could not write to file.');
fclose($file);
*A quick note, you might need to set permissions in order to be able to read/write to the file, but those are specific to the machine type, so if you encounter errors with either do a search for setting permissions for your particular server type.
**Also, 'SELECT * FROM database.table' is going to return ALL of the rows in the entire table. This will generate a very large file, and be a performance hindrance as the table grows. You should use the bean class to update the last saved tuple:
$file = fopen($pathAndNameOfFile, 'a+') or die('Could not open file.');
$query = "SELECT * FROM data_base.table WHERE id = '".$focus->id."';";
$result = $bean->db->query($query,true);
$dbRowData = $bean->db->mysql_fetch_assoc($result);
$printedArray = print_r($dbRowData);
fwrite($file, $printedArray) or die('Could not write to file.');
fclose($file);
You can export/dump mysql databases into SQL files using mysqldump
mysqldump -u userName -p databaseName tableName > fileName.sql

Categories