Hi !
I am trying to create csv file from from mysql.
It has 289+ columns and when i try to open it in excel it doesnt load the whole file corectly. And also I am trying to get the null characters etc.
Here is my code.. .
or a simpler /alternative way to do this .
any help would be appreciated
thanks
<?php
function file_put_contents_php4 ($location, $whattowrite) {
if (file_exists($location)) {
unlink($location);
}
$fileHandler = fopen ($location, "w");
fwrite ($fileHandler, $whattowrite);
fclose ($fileHandler);
}
if(isset($HTTP_POST_VARS['submit'])){
echo "Connecting to database... ";
flush();
// $result = mysql_connect(addslashes($HTTP_POST_VARS['server']), addslashes($HTTP_POST_VARS['username']), addslashes($HTTP_POST_VARS['password']));
$result = mysql_connect("localhost","uname","pass");
if( !$result )
{
echo "<span class=\"error\">FAILED!</span><br />Could not connect to the database, please check your username, password, and server, and try again.</span></body></html>";
flush();
die();
}
echo "<span class=\"success\">Done!</span>";
flush();
echo "<br>";
echo "Selecting database... ";
$result = mysql_select_db("acsets");
if( !$result ){
echo "<span class=\"error\">FAILED!</span><br>Could not select databse, please check your database name and try again.</span></body></html>";
flush();
die();
}
echo "<span class=\"success\">Done!</span>";
flush();
echo "<br>";
echo "Converting table... ";
$output = "";
$addcomma = "false";
$query = mysql_query("SELECT * FROM records");
while($row = mysql_fetch_array($query, MYSQL_ASSOC)){
foreach($row as $x) {
if ($addcomma == "true"){ $output .= ","; }
$x = str_replace("\r\n", "",$x);
$output .= $x;
$addcomma = "true";
}
$output .= "
";
$addcomma = "false";
}
$pos = strpos($HTTP_POST_VARS['csvfile'], ".csv");
/if ($pos == false) {
$csvfile = $HTTP_POST_VARS['csvfile'].".csv";
}
/
$csvfile="demo.csv";
// file_put_contents_php4("/var/www/html/csvfiles/".$csvfile, $output);
file_put_contents_php4("/apache/acsets/".$csvfile, $output);
if( !$query )
{
echo "<span class=\"error\">Oops, something went wrong while converting the table. The exact error MySQL reported was<br>";
echo "MySQL Error Number: " . mysql_errorno();
echo "MySQL Error: " . mysql_error();
echo "</span></body></html>";
die();
}
echo "<span class=\"success\">Done!</span>";
flush();
echo "<br>";
echo "Ok, everything's finished. Now closing the MySQL link.";
mysql_close();
flush();
// print "<br><br><br>Your csv file is now available: <a href=\"http://some/some/".$csvfile."\">".$csvfile."</a>
// <br><br><br><font size=\"2\"><a href=\"mysqlexport.php\" target=\"_self\">Export Another Table</a></font>";
print "<br><br><br>Your csv file is now available: <a href=\"c:/apache/acsets/".$csvfile."\">".$csvfile."</a>
<br><br><br><font size=\"2\"><a href=\"mysqlexport.php\" target=\"_self\">Export Another Table</a></font>";
}
else
{
?>
<form name="form" id="form" action="<?php echo $PHP_SELF; ?>" method="post" onSubmit="return noEntry()">
<table width="623" border="0" align="center" cellpadding="1" cellspacing="0">
<tr>
<td width="621"><div align="center"><span class="heading"><font face="Verdana, Arial, Helvetica, sans-serif"><strong>MySQL
Table To .csv File</strong></font></span><br>
<br>
<table width="363" height="178" border="0" cellpadding="0" cellspacing="0" bordercolor="#000000">
<tr>
<td width="419" height="176" valign="top"><table width="361" border="0" cellspacing="0" cellpadding="0">
<tr valign="middle" background="tinybg2.gif">
<td height="15" colspan="2" background="tinybg2.gif"><div align="center"><font color="#CCCCCC" size="1" face="Verdana, Arial, Helvetica, sans-serif"><strong>Input
Your Database Information Below</strong></font></div>
</td>
</tr>
<!--tr valign="bottom">
<td> Name Of File To Output To: </td>
<td><input type="text" name="csvfile" >
</td>
</tr-->
<tr valign="bottom">
<td height="19" colspan="2"> </td>
</tr>
</table>
</td>
</tr>
</table>
<div align="center"><br>
<input type="submit" name="submit" value="Convert Table" >
<br>
<br>
<font size="1" face="Verdana, Arial, Helvetica, sans-serif"><br>
<br>
</font></div>
</div></td>
</tr>
</table>
</form>
<?php
}
?>