5

I'm trying to insert an accented e into a mysql database following this example. Using this:

mysql_query("SET NAMES 'utf8'");

throws:

Warning: mysql_query() [function.mysql-query]: Access denied for user 'ODBC'@'localhost'

Its not connecting to the database:

DEFINE ('DB_USER', 'user');
DEFINE ('DB_PASSWORD', 'pword');
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'test1');
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
snookian
  • 863
  • 6
  • 13
  • 35
  • 7
    The error says `mysql_error` and your query uses `mysqli_connect` -- is there something fishy? – Amal Murali Jul 22 '13 at 11:48
  • AHHHH Thankyou, how stupid of me. Doesnt solve the issue of saving and e accent into my DB but solves the issue i asked about the connection. Thankyou. Just wondering if you know , it now saves the accent e and a black traingle with a ? in the middle, know why/how to solve this? – snookian Jul 22 '13 at 11:54
  • 1
    Have you tried any of the other things listed at http://stackoverflow.com/questions/279170/utf-8-all-the-way-through , such as saving PHP files as UTF-8 ([without BOM](http://stackoverflow.com/questions/8028957/headers-already-sent-by-php)) and setting an "accept-charset" on any forms? If so, ask a separate question. – PleaseStand Jul 22 '13 at 12:02
  • Thankyou again. Setting the php file to UTf-8 seemed to work. Thankyou – snookian Jul 22 '13 at 13:23
  • Using `@` to suppress errors is usually a bad plan as well. – tadman Jan 08 '15 at 22:09

4 Answers4

9

Don't mix use of the mysql_* and mysqli_* functions. Instead of

mysql_query("SET NAMES 'utf8'");

use

$dbc->set_charset('utf8');
PleaseStand
  • 31,641
  • 6
  • 68
  • 95
2

I think there is no problem in connection there is the the problem that you are using the mysql_query and you are connecting with mysqli_connect.

So use the mysqli_query to execute the query.

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
  • Yes thankyou, Amal Murali already hinted to me this, but thankyou aswell. – snookian Jul 22 '13 at 11:57
  • 1
    For the latest information please read http://dev.mysql.com/doc/apis-php/en/apis-php-mysqli.set-charset.html it tells us the procedural and object oriented ways of conencting and changing to UTF8. – KarlosFontana May 10 '14 at 18:49
0

try something like this:

$dbc = mysql_connect ('localhost', 'user', 'pword');
mysql_select_db('test1',$dbc);
mysql_query('SET NAMES "utf8"',$dbc);
rayryeng
  • 102,964
  • 22
  • 184
  • 193
iGzorn
  • 1
-2

Setting the php file to UTf-8 seemed to work.

Saty
  • 22,443
  • 7
  • 33
  • 51
snookian
  • 863
  • 6
  • 13
  • 35