John Davidson

Encode-Decode issue php (CSV->MySQL)

0 comments
Message:


I'm trying to pull data in CSV format for a server, process and store in a MySQL database:


When I insert into my database, the text is "Atila%27s+Goonies" I want it to be "Atila's Goonies"


I have been reading through all the text encoding topics, but I think I'm missing a very simple function.


My requirement is to have this text and store in the database as a properly formatted (or encoded) string, so I can query later and display online. Database column is collated as "utf8_unicode_ci". I'm not sure if it's relevant because if I want to display on the browser, I cannot get to the correct format either. thanks.


Any help would be appreciated.


My code below:


if (($handle = fopen("alliances.txt", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
$num = count($data);

$row++;
$alliance_id = $data[0];
$alliance_name = $data[1];
$alliance_points = $data[2];
$alliance_villages = $data[3];
$alliance_members = $data[4];
$alliance_rank = $data[5];


$sql_ia = "insert into alliances (alliance_id,alliance_name, alliance_villages,alliance_members,alliance_rank,timestamp)
values (:alliance_id,:alliance_name, :alliance_villages,:alliance_members,:alliance_rank,:timestamp)";
$st_ia = $DBcon->prepare($sql_ia);
$st_ia->bindParam(':alliance_rank', $alliance_rank,PDO::PARAM_INT);
$st_ia->bindParam(':alliance_id', $alliance_id,PDO::PARAM_INT);
$st_ia->bindParam(':alliance_name',$alliance_name,PDO::PARAM_STR);
$st_ia->bindParam(':alliance_members',$alliance_members,PDO::PARAM_INT);
$st_ia->bindParam(':alliance_villages',$alliance_villages,PDO::PARAM_INT);
$st_ia->bindParam(':timestamp',$timestamp,PDO::PARAM_INT);
$author_name = $ag_row[$agency];

$st_ia->execute();

}
fclose($handle);
}


My Answer:
In PHP, when working with CSV files, you may encounter encoding and decoding issues. This can happen when reading or writing CSV files that contain special characters or different character encodings.

To address encoding and decoding issues in PHP when working with CSV files, you can use the `mb_convert_encoding()` function to convert the encoding of the data. Here's an example of how you can use this function to handle encoding and decoding issues in PHP:

php
// Read CSV file
$csvData = file_get_contents('data.csv');

// Convert encoding to UTF-8
$csvData = mb_convert_encoding($csvData, 'UTF-8', 'ISO-8859-1');

// Parse CSV data
$csvRows = str_getcsv($csvData, "\n");

foreach ($csvRows as $row) {
$rowData = str_getcsv($row, ",");

// Process CSV data
// ...
}

// Write CSV file
$csvData = ''; // CSV data to write

// Convert encoding to ISO-8859-1
$csvData = mb_convert_encoding($csvData, 'ISO-8859-1', 'UTF-8');

file_put_contents('output.csv', $csvData);


In this example, we first read the CSV file and convert its encoding to UTF-8 using `mb_convert_encoding()`. We then parse the CSV data and process it as needed. When writing the CSV file, we convert the encoding back to ISO-8859-1 before saving the data.

By using `mb_convert_encoding()` to handle encoding and decoding issues in PHP when working with CSV files, you can ensure that special characters and different character encodings are handled correctly.

Rate this post

3 of 5 based on 5606 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us