0

i have a problem, with the German "Umlaute" "ä, ü, ö, ß" in a sql database.

There is a php-file in which i open a sql file and restore a whole database. But after every restore all german special letters are wrong (something like "ß"). When I look into the sql file there they are writte as they should.

Here ist my php file:

// Name of the file
$filename = 'backup.sql';
// MySQL host
$mysql_host = '...';
// MySQL username
$mysql_username = '...';
// MySQL password
$mysql_password = '...';
// Database name
$mysql_database = '...';

//////////////////////////////////////////////////////////////////////////////////////////////

// Connect to MySQL server
mysql_connect($mysql_host, $mysql_username, $mysql_password) or die('Error connecting to     MySQL server: ' . mysql_error());
// Select database
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());

// Temporary variable, used to store current query
$templine = '';
// Read in entire file
$lines = file($filename);
// Loop through each line
foreach ($lines as $line)
{
    // Skip it if it's a comment
    if (substr($line, 0, 2) == '--' || $line == '')
    continue;

    // Add this line to the current segment
    $templine .= $line;
    // If it has a semicolon at the end, it's the end of the query
    if (substr(trim($line), -1, 1) == ';')
    {
    // Perform the query
    mysql_query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . mysql_error() . '<br /><br />');
    // Reset temp variable to empty
    $templine = '';
    }
}

My SQL File (backup.sql) looks like this:

-- phpMyAdmin SQL Dump
-- version 3.5.8
-- http://www.phpmyadmin.net
--
-- Host: ...
-- Erstellungszeit: 10. Mai 2013 um 13:57
-- Server Version: 5.5.28
-- PHP-Version: 5.2.17

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";
SET time_zone = "+00:00";

--
-- Datenbank: `...`
--

--
-- !!!!!!!!!!!!!!!!!  Delete all !!!!!!!!!!!!!!!!!!!!!
--

TRUNCATE TABLE `benutzer`;
TRUNCATE TABLE `nachrichten`;
TRUNCATE TABLE `programme`;
TRUNCATE TABLE `tagebuch`;
TRUNCATE TABLE `therapeuten`;
TRUNCATE TABLE `trainingseinheiten`;

REPLACE INTO `benutzer` (`ID`, `EMail`, `Kennwort`, `passwort`, `salt`, `MandantenArt`, `Vorname`, `Nachname`, `Firma`, `Strasse`, `PLZ`, `Ort`, `Land`, `UID`, `Telefon`, `Fax`, `Webadresse`, `Notizen`, `PT_ID`, `Erinnerung`, `Onlinebetreut`, `Therapeut`, `Diagnose`, `Erstellungsdatum`, `Letzter_Login`, `Deleted`, `Status`) VALUES
...
(74, 'demopat@physio-visor.at', 'yes', '...', '...', 'pa', 'Max', 'Mustermann', '', 'Landstraße 11', '6020', 'Innsbruck', 'Österreich', '', '0123 456 789', '', '', '', '69', 'nein', 'ja', 'keiner zugeteilt', '', '2013-06-11 19:56:21', '2013-06-11 20:01:14', 0, 0);

This is only a shortcut of the sql file. But you can see there the "ß" and the "Ö" for example.

In my MSQL Database the tables are all utf8_general_ci.

Can you tell me, thy this german special letters aren't right on website?

lackner_media
  • 153
  • 5
  • 20

1 Answers1

2

Seems like you don't set your mysql connection to utf-8:

$link = mysql_connect($mysql_host, $mysql_username, $mysql_password) 
        or die('Error connecting to     MySQL server: ' . mysql_error());
mysql_query("SET character_set_results=utf8", $link);
mysql_select_db($mysql_database) or die('Error selecting MySQL database: ' . mysql_error());
mysql_query("set names 'utf8'",$link);
Aris
  • 4,643
  • 1
  • 41
  • 38
  • you are my HERO ... the solution can be that easy. Thanks a lot not everything works fine. – lackner_media Apr 10 '14 at 20:21
  • 2
    @fomlackner thanks. I suggest you use mysqli or even PDO however. mysql library is deprecated and will be removed in the future. – Aris Apr 11 '14 at 14:05