I'm struggeling with a tiny script which is responsible for 2 things: - truncating database - uploading files into database
Looks like that:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
$mysql_host = 'localhost';
$mysql_username = 'x';
$mysql_password = 'y';
$mysql_database = 'z';
$db = new PDO('mysql:dbname='.$mysql_database.';host='.$mysql_host,$mysql_username,$mysql_password);
// works not with the following set to 0. You can comment this line as 1 is default
$db->setAttribute(PDO::ATTR_EMULATE_PREPARES, 1);
function truncate_db()
{
global $db;
$sql_query_1 = "
TRUNCATE TABLE `VISITS`;
TRUNCATE TABLE `ANIMALS`;
TRUNCATE TABLE `DOCTORS`;
TRUNCATE TABLE `PAYMENTS`;
TRUNCATE TABLE `CUSTOMER`
";
try {
$stmt = $db->prepare($sql_query_1);
$stmt->execute();
echo "Truncate action - OK";
}
catch (PDOException $e)
{
echo $e->getMessage();
die();
}
}
function import_db()
{
global $db;
try
{
$sql_query_2 = implode(array_map(function ($v) {
return file_get_contents($v);
}, glob(__DIR__ . "/*.sql")));
$qr = $db->exec($sql_query_2);
echo "Import action - OK";
}
catch (PDOException $e)
{
echo 'Connection failed: ' . $e->getMessage();
}
}
truncate_db();
echo '<br />';
import_db();
$db = null;
?>
Issue - files (sql one) which I'm uploading to the database contains special charaters (like ś, ó, ę etc.) After that I have an issue in the database that some of the words doesn't contain any more those symbols. After upload I have symbols like: ³, ¿ etc. How can I edit function import_db() to keep those characters? i thought about:
mb_convert_encoding
but I have no clue how to incorporate that into my code ;/ in my DB table, column with that words (containing special characters) is set to: UTF8_General_CI. thanks!