2

Iam trying to read a data from mysql database. In my db i checked the data is showing as 'μ' and charset is properly set to 'UTF-8'. But when in PHP when i ran this below code, the output 'μ' is showing as '?'. I tried so many ways by adding different types of charsets and all in the scripts, but all in vain, still its showing as '?'. Can anyone pls help me with it.

My script is below:

<?php
  ini_set("default_charset", "UTF-8");
  header('Content-type: text/html; charset=UTF-8');

  error_reporting(E_ALL);
  ini_set("display_errors", 1);

  $tender_id=$_GET['tender_id'];

  include('config_sqli.php');
?>
<?php
  $result=mysqli_query($con,"SELECT * FROM comparitive_st_sup WHERE 
                      id='$member[$x]' and tender_id='$tender_id'");
  while($row=mysqli_fetch_assoc($result)){

  $prod_description1 = $row['prod_description'];
  $prod_description = mysqli_real_escape_string($con, $prod_description1);
  }
  echo $prod_description;
  echo '<br>';
?>
Dave
  • 3,073
  • 7
  • 20
  • 33
Sanju Menon
  • 747
  • 1
  • 10
  • 24
  • try utf8_encode:http://php.net/manual/en/function.utf8-encode.php – Dave Sep 12 '16 at 06:00
  • i saw something like $value = utf8_encode($value); I tried to implement in my code but its showing ?? only. Need to check what is the exact problem. – Sanju Menon Sep 12 '16 at 06:05
  • You need to set the encoding in `mysql` as well. Take a look at http://stackoverflow.com/questions/14068616/php-mysql-charset-utf8-problems – Peter Sep 12 '16 at 06:27
  • 2
    **WARNING**: When using `mysqli` you should be using [parameterized queries](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) and [`bind_param`](http://php.net/manual/en/mysqli-stmt.bind-param.php) to add user data to your query. **DO NOT** use string interpolation or concatenation to accomplish this because you have created a severe [SQL injection bug](http://bobby-tables.com/). **NEVER** put `$_POST` or `$_GET` data directly into a query, it can be very harmful if someone seeks to exploit your mistake. – tadman Sep 12 '16 at 07:25
  • When displaying HTML use `htmlspecialchars` and not SQL escaping. You also want to move that `echo $prod_description` inside the loop, or remove the loop entirely and just fetch once. – tadman Sep 12 '16 at 07:26
  • 1
    Thank you for the info @tadman. Appreciate that. Surely implement that. – Sanju Menon Sep 12 '16 at 09:44
  • `UTF8` is ***not*** the correct character set for MySQL, you want to be using `UTF8mb4`. Please [read this to found out why](http://stackoverflow.com/q/279170/3536236) – Martin Sep 20 '16 at 12:04

1 Answers1

4

try this,

Run below query before executing actual query:

mysqli_query($con, "SET NAMES 'utf8'");

OR

try this your mysqli connection

$mysqli = new mysqli("localhost", "root", "", "test");
$mysqli->set_charset("utf8"); 
// be careful, this method can be not working (mantainers builds bugs for old php releases), first example is works fine

i hope it will be helpful.

Dave
  • 3,073
  • 7
  • 20
  • 33