Solved

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

Posted on 2016-09-02
7
125 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 110

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 110

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
The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

 
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 110

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 110

Expert Comment

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

Featured Post

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Group By Function Required in PDF Output 21 65
How to close my php post page. 3 41
MySql Qry performance  (Ver 5.5.17) 1 16
php time 12 24
Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
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
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

734 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