Solved

mysql question - @mysql_query("SET NAMES 'utf8'");

Posted on 2016-09-02
7
48 Views
Last Modified: 2016-09-02
$sql = "INSERT INTO mesaj (uid,ipi,yorum,tarih) VALUES ('$uid','$ipi','$comment','$todayis') ";
$result = $conn->query($sql);
@mysql_query("SET NAMES 'utf8'");
}
I have inserting some chracters to my database like ç,ö,ş,ü,ğ
I heard that @mysql_query("SET NAMES 'utf8'"); make things right

but I'm not sure where to put the code?
is it correct to write it like above?
0
Comment
Question by:Braveheartli
  • 4
  • 2
7 Comments
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41781427
Short answer, "no" -- instead use something like the methods documented here:

See Character Sets in MySQL and also Using UTF-8 with PDO
https://www.experts-exchange.com/articles/11880/Unicode-and-Character-Collisions.html
0
 
LVL 30

Expert Comment

by:Marco Gasi
ID: 41781430
First you should leave mysql (is deprecated: http://php.net/manual/en/migration55.deprecated.php) and switch to mysqli.

That done, you should set utf-8 for the whole database when you establish the connection:

$conn = new mysqli($db_host, $db_username, $db_password, $db_name);
$conn->set_charset("utf8mb4");

Open in new window

0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41781432
NEVER write anything that uses the MySQL extension again!

Some help on migrating from the obsolete MySQL extension to one of the supported extensions is available in this article:
https://www.experts-exchange.com/articles/11177/PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html

But that said, there is a little more to it than just changing the DB character set.  You need consistency across all parts of the platform, including the database, the PHP scripts, and the HTML document.  The code examples in the Unicode article are all tested and seem to work well.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 1

Author Comment

by:Braveheartli
ID: 41781454
is it better in this way?

// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

$sql = "INSERT INTO mesaj (uid,ipi,yorum,tarih) VALUES ('$uid','$ipi','$comment','$todayis') ";

if (mysqli_query($conn, $sql)) {
    echo "Mesajınız alınmıştır";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 41781466
Yes, it is better to use MySQLi, and not MySQL.  But the code you posted above does not specify UTF-8.  You can find all the details in the article!
https://www.experts-exchange.com/articles/11880/Unicode-and-Character-Collisions.html

Try it like this:
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}

// SET THE CHARACTER SET
mysqli_set_charset($conn, "utf8mb4");

$sql = "INSERT INTO mesaj (uid,ipi,yorum,tarih) VALUES ('$uid','$ipi','$comment','$todayis') ";

if (mysqli_query($conn, $sql)) {
    echo "Mesajiniz alinmistir";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($conn);
} 

Open in new window

0
 
LVL 1

Author Closing Comment

by:Braveheartli
ID: 41781470
thank you very much Ray Paseur
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 41781625
Glad to help! :-)
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now