Asked  7 Months ago    Answers:  5   Viewed   39 times

I have a table with some text and text is greek letters, when i use sql tool and select the data from this table is showing correctly Image for SQL Query Results

But when i show this my site frontend using mysql_fetch array and when echo it shows as below enter image description here

Anyone know how to fix this error thank you.

 Answers

19

try the following:

after you connect to the mysql, do this query to make sure that you are using UTF8:

mysql_query("SET NAMES 'utf8'");
mysql_query("SET CHARACTER SET 'utf8'");

make sure that in HTML (head) you are using the right encoding, try:

<meta http-equiv="content-type" content="text/html; charset=UTF-8">

if this does not help, try different encoding, like ISO-8859-1

Wednesday, March 31, 2021
 
inieto
answered 7 Months ago
86

Using something like this before Your query to set charset of client and results:

  mysql_query("SET character_set_results = 'utf8', character_set_client = 'utf8', character_set_connection = 'utf8', character_set_database = 'utf8', character_set_server = 'utf8'", $conn);
Wednesday, March 31, 2021
 
msg
answered 7 Months ago
msg
40

Don't store a comma-delimited list of attributes in a database column.

Instead, have 3 tables:

Movies (id, movie_name)

id | movie_name
---+--------------------------------------
 1 | Die Hard
 2 | Gladiator
 3 | Harry Potter and the Sorcerers Stone
 4 | Pearl Harbor

Genres (id, genre_name)

id | genre_name
---+------------
 1 | Action
 2 | Thriller
 3 | Adventure
 4 | Drama
 5 | History
 6 | Fantasy
 7 | Family
 8 | Melodrama
 9 | War

MovieGenre (movie, genre)

Movie | Genre
------+-------
    1 | 1
    1 | 2
    2 | 1
    2 | 3
    2 | 4
    2 | 5
    3 | 3
    3 | 6
    3 | 7
    4 | 1
    4 | 8
    4 | 9

Then your problems become much, much simpler.

Wednesday, March 31, 2021
 
Markol
answered 7 Months ago
31

Change your mysql database/ table / column encoding to UTF-8 (and also set the collation to a compatible value).

ALTER TABLE mytable CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
ALTER TABLE mytable 
MODIFY country CHAR(50) 
CHARACTER SET utf8 COLLATE utf8_general_ci;

Also specify the char set at the PHP side when connecting.

mysql_set_charset('utf8',$conn); 

Take a look at this article for further info and a script to batch change every table / column in a database.

Saturday, May 29, 2021
 
Gilko
answered 5 Months ago
86

The problem is probably that $this->phone is empty when you run the query.

In that case, the query will be SELECT * FROM [...] OR WHERE phone LIKE '%%' which will always return everything.

Solution: leave out the phone criteria if not provided or (hack alert!) use a value that will never occur in that column.

Another way to do this is changing the query to something like

SELECT *
  FROM directory
 WHERE name LIKE :name 
   AND :name_provided = 1
    OR phone LIKE :phone
   AND :phone_provided = 1

And then bind :phone_provided to 1 if $this->phone is defined, 0 otherwise. Likewise with :name_provided.

Saturday, May 29, 2021
 
hillz
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :