?
Solved

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

Posted on 2016-09-02
7
Medium Priority
?
233 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 111

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 31

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 111

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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

Expert Comment

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

Featured Post

NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, we’ll look at how to deploy ProxySQL.
The title says it all. Writing any type of PHP Application or API code that provides high throughput, while under a heavy load, seems to be an arcane art form (Black Magic). This article aims to provide some general guidelines for producing this typ…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month16 days, 2 hours left to enroll

850 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