Non-ASCII glyphs/characters issue with server-side processing and mysqli

Non-ASCII glyphs/characters issue with server-side processing and mysqli

CapamaniaCapamania Posts: 230Questions: 80Answers: 5
edited August 2015 in Free community support

I have issues with Non-ASCII glyphs/characters (e.g. http://terpconnect.umd.edu/~zben/Web/CharSet/htmlchars.html ) in my server-side table. The below mysqli script is working fine, yet if there are Non-ASCII glyphs/characters in the database table, the rows with those characters are either not displayed, messes up the sorting/filtering or no content at all will be displayed. Does somebody know what might be the issue here?!

```
<?php
/* Database connection start /
$servername = "localhost";
$username = "root";
$password = "Password1";
$dbname = "test";
$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());
/
Database connection end */
// storing request (ie, get/post) global array to a variable
$requestData= $_REQUEST;
$columns = array(
// datatable column index => database column name
0 =>'employee_name',
1 => 'employee_salary',
2=> 'employee_age'
);
// getting total number records without any search
$sql = "SELECT employee_name, employee_salary, employee_age ";
$sql.=" FROM employee";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalData = mysqli_num_rows($query);
$totalFiltered = $totalData; // when there is no search parameter then total number rows = total number filtered rows.
if( !empty($requestData['search']['value']) ) {
// if there is a search parameter
$sql = "SELECT employee_name, employee_salary, employee_age ";
$sql.=" FROM employee";
$sql.=" WHERE employee_name LIKE '".$requestData['search']['value']."%' "; // $requestData['search']['value'] contains search parameter
$sql.=" OR employee_salary LIKE '".$requestData['search']['value']."%' ";
$sql.=" OR employee_age LIKE '".$requestData['search']['value']."%' ";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");
$totalFiltered = mysqli_num_rows($query); // when there is a search parameter then we have to modify total number filtered rows as per search result without limit in the query
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." "; // $requestData['order'][0]['column'] contains colmun index, $requestData['order'][0]['dir'] contains order such as asc/desc , $requestData['start'] contains start row number ,$requestData['length'] contains limit length.
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees"); // again run query with limit

} else {
$sql = "SELECT employee_name, employee_salary, employee_age ";
$sql.=" FROM employee";
$sql.=" ORDER BY ". $columns[$requestData['order'][0]['column']]." ".$requestData['order'][0]['dir']." LIMIT ".$requestData['start']." ,".$requestData['length']." ";
$query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");

}
$data = array();
while( $row=mysqli_fetch_array($query) ) { // preparing an array
$nestedData=array();
$nestedData[] = $row["employee_name"];
$nestedData[] = $row["employee_salary"];
$nestedData[] = $row["employee_age"];

$data[] = $nestedData;

}
$json_data = array(
"draw" => intval( $requestData['draw'] ), // for every request/draw by clientside , they send a number as a parameter, when they recieve a response/data they first check the draw number, so we are sending same number in draw.
"recordsTotal" => intval( $totalData ), // total number of records
"recordsFiltered" => intval( $totalFiltered ), // total number of records after searching, if there is no searching then totalFiltered = totalData
"data" => $data // total data array
);
echo json_encode($json_data); // send data as json format

<?php > ``` ?>

Answers

  • CapamaniaCapamania Posts: 230Questions: 80Answers: 5

    Edit: I'm using collation "latin1_swedish_ci" in my mysql database. Could this be the issue?!

  • CapamaniaCapamania Posts: 230Questions: 80Answers: 5

    Here is the solution. I needed to convert ISO-8859-1 back to UTF-8 like that ...

    $nestedData[] = iconv("ISO-8859-1", "UTF-8", $row["employee_name"]);
    $nestedData[] = iconv("ISO-8859-1", "UTF-8", $row["employee_salary"]);
    $nestedData[] = iconv("ISO-8859-1", "UTF-8", $row["employee_age"]);
    
  • iqbalhassaniqbalhassan Posts: 1Questions: 0Answers: 0

    in 33,26
    $query=mysqli_query($conn, $sql) or die("employee-grid-data.php: get employees");

    what does get employees means

This discussion has been closed.