user3464303
user3464303

Reputation: 85

How to change the default character set for the database connection?

I cannot enter(via php) Turkish characters correctly into PhpMyadmin. I set everything(browser encoding, html charset, database collation en connection) to UTF-8..

and my query

<?php
  ob_start();
  session_start();

  mysql_connect('localhost', 'root', 'pass');
  mysql_select_db('myDatabase');
  mysql_query("SET NAMES 'utf8' COLLATE 'utf8_turkish_ci'");
?> 

P.S:As I change the text on database and write it correctly, I can display them normal on the browser

So the problem is entering text via php correctly into database..

I used msqli to find what the problem was:

<?php
  ob_start();
  session_start();
  $link = mysqli_connect('localhost', 'root', 'pass');
  mysql_select_db('myDatabase');
  mysql_query("SET NAMES 'utf8' COLLATE 'utf8_turkish_ci'");

  /* check connection */
      if (!$link) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
  }

 /* Print current character set */
      $charset = mysqli_character_set_name($link);
      printf ("Current character set is %s\n",$charset);

 /* close connection */
      mysqli_close($link);

 ?> 

output(the default character set for the database connection) is "Current character set is latin1"

1- I can't find that default 'latin1' in php.ini or somewhere else..where does it come from?

2- How can I change that to utf8_turkish_ci?

Thank you in advance!

Upvotes: 0

Views: 891

Answers (1)

Fabricator
Fabricator

Reputation: 12782

mysql and mysqli libraries create separate mysql sessions, to verify your charset setting in the mysql session, you can do the following:

mysql_select_db('myDatabase');
mysql_query("SET NAMES 'utf8' COLLATE 'utf8_turkish_ci'");
$rs = mysql_query("show variables like 'char%'");
$row = mysql_fetch_assoc($rs);
print_r($row);

Upvotes: 1

Related Questions