Solved

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

Posted on 2016-09-02
7
147 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
This article discusses four methods for overlaying images in a container on a web page
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…
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.

707 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