I am trying to create a csv file from a table in a mysql database using the following code. No errors come up, but nothing happens either...
I know that i am connected to the database properly because earlier in my code a number of tables from the database are selected and that works fine. the first line is simply to make the script wait to act until i have submitted the form. The echo statements seem to be outputting what i want, but this file doesn't work.

if (isset($_POST['add_item'])){
	$table = $_POST['tName'];
	$sql = 'SELECT * INTO OUTFILE "csv/unconvert_tester.csv" FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY '. mysql_real_escape_string('"') .' LINES TERMINATED BY "\n" FROM '."$table";
	$result = mysql_query("$sql");
	echo("$sql <br>");
	echo("file created for $table");
}

Recommended Answers

All 11 Replies

Why are you using mysql_real_escape string for your quote but not for $table which is coming directly from POST?. Also what do you mean the file doesn't work, is the csv file empty or what?

i mean it doesn't work as in no file is created. The csv folder is still empty. When i run it this is the statement that comes from echoing the sql statement (the first echo):

SELECT * INTO OUTFILE "csv/unconvert_tester.csv" FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY \" LINES TERMINATED BY "\n" FROM convtest

note that convtest is a table in my database and it does have data in it.

$result = mysql_query($sql);
if (!$result) {
  die(mysql_error());
}

That'll tell you if the query is actually executing or not. And you don't have to surround variables with "" if that's the only thing in the statement. mysql_query($sql); or 'some string' . $variable are both fine and the preferred way to do it.

alright, at least an error message comes up...
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '\" LINES TERMINATED BY "\n" FROM convtest' at line 1

OPTIONALLY ENCLOSED BY '"' get rid of that completely unnecessary mysql_real_escape_string('"') BS

alright so i dumbed it down a little to just include $sql = 'SELECT * INTO OUTFILE "csv/unconvert_tester.csv" FROM '.$table; and i got this message
Can't create/write to file 'c:\wamp\bin\mysql\mysql5.1.33\data\csv\unconvert_tester.csv' (Errcode: 2)
im not sure where the "c:\wamp\bin\mysql\mysql5.1.33\data\" part of the file path is coming from. I am doing this on a local server.

You're using a relative path "csv/unconvert_tester.csv" so MySQL seems to be placing it in a default location for outfiles. Just specify an absolute path (starting with /) to tell it the exact location.

ok so it kinda worked, the file is in there, which was my biggest problem, but obviously since i took out the formatting stuff its all a mess. Now i just need to get this stuff right.

FIELDS TERMINATED BY "," OPTIONALLY ENCLOSED BY \" LINES TERMINATED BY \n

which is located between
SELECT * INTO OUTFILE "c:/wamp/www/project/csv/tester.csv"
and
FROM '.$table;

It looks like your missing a double quote, to me.

I already gave you an example for that: OPTIONALLY ENCLOSED BY "\"" LINES TERMINATED BY "\n" I would point you to the documentation but evidently there is a major power outage in Sweden right now and MySQL's servers are down (not joking)

AWESOME!!!!!! Finally. Thanks Shawn, you really saved me there. You're the man

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.