Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

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

Posted on 2016-09-02
7
Medium Priority
?
205 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This article shows the steps required to install WordPress on Azure. Web Apps, Mobile Apps, API Apps, or Functions, in Azure all these run in an App Service plan. WordPress is no exception and requires an App Service Plan and Database to install
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

618 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