https://www.phpclasses.org/blog/post/571-php-xlsx-mysql.html
上記URLの説明通りにPHPを作成してみました。
include 'simplexlsx.class.php'; $xlsx = new SimpleXLSX( 'countries_and_population.xlsx' ); try { $conn = new PDO( "mysql:host=localhost;dbname=mydb", "user", "pass"); $conn->setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); } catch(PDOException $e) { echo $sql . "<br>" . $e->getMessage(); } $stmt = $conn->prepare( "INSERT INTO countries_and_population (rank, country, population, date_of_estimate, powp) VALUES (?, ?, ?, ?, ?)"); $stmt->bindParam( 1, $rank); $stmt->bindParam( 2, $country); $stmt->bindParam( 3, $population); $stmt->bindParam( 4, $date_of_estimate); $stmt->bindParam( 5, $powp); foreach ($xlsx->rows() as $fields) { $rank = $fields[0]; $country = $fields[1]; $population = $fields[2]; $date_of_estimate = $fields[3]; $powp = $fields[4]; $stmt->execute(); }
上記サンプルの通りに作成してみたら、動作自体はきちんと動作したのですが、登録されたデータが文字化けしていました。
- $fields[0]
- $fields
- $rank
- $xlsx
に対して「mb_convert_encoding($str,"utf8")」をかけても文字化けしてしまいます。
simplexlsx.class.php のソースをみたら、mb_strlenなどの記述に「8bit」という記述があったので、「16bit」に書き換えても変化しませんでした。(多分これは違うとも感じました。)
データベース登録時の文字化け解消法を教えていただきたく、お願い致します。












回答2件
あなたの回答
tips
プレビュー