Imrul.H
Imrul.H

Reputation: 5870

php mysql search with special character

I have a table in mysql DB which contains special character like Ø,Æ,etc. I cannot find these fields when i run a search with php. but when i run the same sql in phpmyadmin, i get results. this is the table structure:

CREATE TABLE IF NOT EXISTS `clientinfo` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `adresse` varchar(160) NOT NULL,
  `gatenavn` varchar(20) NOT NULL,
  `husnr` varchar(20) NOT NULL,
  `bokstav` varchar(2) NOT NULL,
  `postnr` varchar(20) NOT NULL,
  `poststed` varchar(60) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8 AUTO_INCREMENT=398 ;

This is a sample query:

SELECT * FROM clientinfo WHERE gatenavn = 'EKRAVEIEN' AND husnr = '1' AND postnr = '2010' AND poststed = 'STRØMMEN'

when i run this query in phpmyadmin, i get result; but don't get when i run with php. I am using mysqli. need some help.

Upvotes: 0

Views: 2287

Answers (5)

martinstoeckli
martinstoeckli

Reputation: 24151

Tell your connection instance, to deliver UTF-8, before making queries. In MySqli you can call the function set_charset(), afterwards the connection object will deliver UTF-8.

Calling this function makes you independent of the database configuration, if necessary the connection will convert the returned data. Of course it is fastest if no conversion is necessary, so adjusting the configuration is a good thing too.

// tells the mysqli connection to deliver UTF-8 encoded strings.
$db = new mysqli($dbHost, $dbUser, $dbPassword, $dbName);
$db->set_charset('utf8');

Upvotes: 3

BudwiseЯ
BudwiseЯ

Reputation: 1826

If you have access to your MySQL configuration file, set these settings to my.cfg:

[client]
default-character-set=utf8

[mysql]
default-character-set=utf8

[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8

Upvotes: 0

cipher
cipher

Reputation: 2484

This could be a issue with your php.ini file, your Apache Webserver, or charset of your HTML Document or your MySQL

Couple of things to to:

Always ensure that your charset is set to utf8 in your html meta tags

Set

default_charset = "utf-8";

in your php.ini file

And add

AddDefaultCharset UTF-8

to your httpd.conf if you are outputting unicode chars

Upvotes: -1

Marcassin
Marcassin

Reputation: 1405

Try using htmlspecialchars php function.

string htmlspecialchars ( string $string , int $flags = ENT_COMPAT | ENT_HTML401
                         , string $encoding = 'UTF-8' , bool $double_encode = true)

You can change the encoding info of your string. Eg KOI8-R for Russian symbols

Upvotes: -1

Stephan
Stephan

Reputation: 8090

Try setting :

SET NAMES utf8;

before your query in the same mysql session

Upvotes: 2

Related Questions