shades Posted October 19, 2017 Share Posted October 19, 2017 Hi, I got a string with special characters and in the table when i see the text, all the texts are inserted properly with the special characters. But when I use the below code to generate CSV file, for few of the special characters like - , ' and ... it displays –, ’ and …. So could someone guide me what changed I have to do in order to get the characters get displayed properly in my CSV file ? <?php include("dbconnect.php"); if(isset($_POST['groupname'], $_POST['decks'], $_POST['rows'])){ $groupname = $_POST['groupname']; $decks = $_POST['decks']; $rows = $_POST['rows']; $pickdeckrows = $rows/$decks; $zip = new ZipArchive(); $zipname = $groupname.'.zip'; $zip -> open($zipname, ZipArchive::CREATE); $j=1; for($i=1; $i<=$decks; $i++){ $stmt = $dbconnect->prepare("SELECT v.deckid, LPAD(v.decknumber,3,0) as decknumber, v.text FROM texttable v WHERE v.groupname=:groupname ORDER BY v.id LIMIT :pickdeckrows"); $stmt->bindValue(':groupname', $groupname); $stmt->bindValue(':pickdeckrows', $pickdeckrows); $stmt->execute(); $output = fopen('php://temp/maxmemory:1048576', 'w'); if (false === $output) { die('Failed to create temporary file'); } while($row = $stmt -> fetch()){ $length += fputcsv($output, [$groupname.$i.$row["decknumber"], $row["text"] ]); } rewind($output); $zip->addFromString($groupname.'Deck '.$j.'.csv', stream_get_contents($output) ); fpassthru($output); fclose($output); $j++; } $zip->close(); /* To download the zip file from browser use below commented code */ header('Content-Type: application/zip'); header('Content-disposition: attachment; filename='.$zipname); header('Content-Length: '. filesize($zipname)); header('Expires: 0'); header('Cache-Control: private'); header('Pragma: private'); ob_clean(); flush(); readfile($zipname); unlink($zipname); } else { echo "Not set!!!"; } ?> Thanks. Link to comment Share on other sites More sharing options...
cloetensbrecht Posted October 19, 2017 Share Posted October 19, 2017 Have you added the following mysql query after your db connection? SET NAMES utf8 Link to comment Share on other sites More sharing options...
shades Posted October 20, 2017 Author Share Posted October 20, 2017 Have you added the following mysql query after your db connection? SET NAMES utf8 Right now I am using the below db connection. PHP version is 5.6 $host = ''; $db = ''; $user = ''; $pass = ''; $charset = 'utf8mb4'; $dsn = "mysql:host=$host;dbname=$db;charset=$charset"; $opt = [ PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_EMULATE_PREPARES => false, ]; $dbconnect = new PDO($dsn, $user, $pass, $opt); So, i guess it should work if i change to utf8, but i did change and check the below string: ~ @ ! # $ % ^ & * ( ) _ + } { " | \ /.,\ ?>|": but in my CSV file it shows : ~ @ ! # $ % ^ & * ( ) _ + } { " | \ /.,\ ?>|": Link to comment Share on other sites More sharing options...
cloetensbrecht Posted October 21, 2017 Share Posted October 21, 2017 html_special_chars_decode($row[“text”]) Link to comment Share on other sites More sharing options...
shades Posted October 21, 2017 Author Share Posted October 21, 2017 html_special_chars_decode($row[“text”]) Thanks that did the job. Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.