sys_debug
sys_debug

Reputation: 4003

php unable to read characters in utf8

I am trying to do the simple following:

<?php
 header("Content-Type: text/html;charset=UTF-8");

$con=mysqli_connect("localhost","dsdsds","test1234","dsdsds");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }


$result = mysqli_query($con,"SELECT * FROM discounts");

while($row = mysqli_fetch_array($result))
  {
  echo $row['name'] . " " . $row['rate'];
  echo "<br>";
  }




?>

When I do this, my output shows as ??????. in the database table, it is showing as Arabic correctly. What am I doing wrong? also I know I should add code to set mysqli to utf8 and when I do, it breaks script.

  if (!$mysqli->set_charset("utf8")) {
        printf("Error loading character set utf8: %s\n", $mysqli->error);
    } else {
        printf("Current character set: %s\n", $mysqli->character_set_name());
    }

Looking forward to your support

Upvotes: 2

Views: 196

Answers (1)

000
000

Reputation: 27247

mysqli has two ways to call methods. Procedural and Object-oriented. You'll see in the documentation both methods.

This is the procedural style:

$con=mysqli_connect...
$result = mysqli_query...
$row = mysqli_fetch_array...

This is the OO style:

$mysqli = new mysqli("localhost"...
$mysqli->set_charset("utf8")
$result = $mysqli->query...
$row = $mysqli->fetch_array...

It looks like you are mixing the styles. Change $mysqli->set_charset to $mysqli_set_charset and try again.

If you would like my opinion, I prefer the OO style.

Upvotes: 4

Related Questions