0

enter image description here

When reading and displaying data which is in cyrilic from a Microsoft SQL Server, PHP fails to display it correctly. You can see part of the text displayed (only the non cyrilic stuff)

Here is what I have tried:

  • setting the values in the table nchar, varchar, text and char
  • adding a utf-8 tag in the html code
  • using the PHP driver for SQL with the ODBC driver
  • tried these collations: Cyrillic_General_100_CI_AI_SC_UTF8, cyrillic general with utf-8 and the windows code page (1251) with cyrillic general

enter image description here

enter image description here

<html>
<style>
table { width: 20em; border-collapse: collapse; }

th {
  border-bottom: 2px solid #000;
  padding: 0.5em 0 0.1em 0;
  font-size: 1.2em;
}

td {
  border-bottom: 2px solid #ccc;
  padding: 0.5em 0 0.1em 0;
}

th:nth-child(n + 2),
td:nth-child(n + 2) {
  text-align: center;
}

[data-has-link="no"]    { background-color: #F77; }
[data-has-link="yes"]   { background-color: #7F7; }


#score, #name {  width: 50%; }
</style>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="jo.js"></script>
<title>AV.31.U</title>
</head>

<body>
<table style="width: 100%; height:50%" border="1" cellpadding="3">
<caption>AV.31.U</caption>
  <thead>
<tr>
            <td><div id="current_date"></p>
<script>
$(function() {
  var $table = $('table');
  $table.find('#current_date').text(getCurrentDate());
  colorRows($table); // Color the rows!
});

function getCurrentDate () {
  return new Date().toLocaleDateString('en-GB', {
    year: 'numeric',
    month: 'numeric',
    day: 'numeric'
  });
}

function colorRows($table) {
  var hasLink;
  $table.find('tbody > tr').each(function(rowIndex) {
    const $row = $(this);
    $row.find('td').each(function(colIndex) {
      const $cell = $(this).removeAttr('data-has-link');
      const cellValue = $cell.text().trim();
      if (isFinite(cellValue)) {
        // Color cell based on individual data
        const hasLink = cellHasLink(parseInt(cellValue, 10));
        if (hasLink !== 'maybe') {
          $cell.attr('data-has-link', hasLink);
        }
      }
    });
    // Color row based on 7th column
    var i = parseInt($row.find('td:nth-child(7)').text(), 10);
    $row.attr('data-has-link', cellHasLink(i));
  });
}

function cellHasLink(value) {
  switch (value) {
    case 0  : return 'no';
    case 1  : return 'yes';
    default : return 'maybe';
  }
}
</script></td>
    <td colspan="6">Home</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <th>Наименование</th>
      <th>Описание</th>
      <th>Версия</th>
      <th>Верс. опис.</th>
      <th>Модел</th>
      <th>Видео</th>
      <th>Видео отвори</th>
      <th>Снимк.</th>
      <th>Снимк. отвори</th>
      <th>Специф.</th>
      <th>Специф. отвори</th>
    </tr>
  </thead>
  <tbody>
    <tr>
<?php
$username = 'censored';
$password = 'censored';
$servername = 'censored';
$database = 'SQL_KASI';
ini_set('display_errors', '1');
error_reporting(E_ALL);

$db = odbc_connect("Driver={SQL Server};Server=$servername;Database=$database;", $username, $password) or die ("could not connect<br />");

$stmt = "Select * from machine_vertical";
$result = odbc_exec($db, $stmt);

if ($result == FALSE) die ("could not execute statement $stmt<br />");

while (odbc_fetch_row($result)) // while there are rows
{
    print "<tr>\n";
    print "  <td>" . odbc_result($result, "mach_name") . "\n";
    print "  <td>" . odbc_result($result, "mach_descr") . "\n";
    print "  <td>" . odbc_result($result, "mach_version") . "\n";
    print "  <td>" . odbc_result($result, "mach_version_descr") . "\n";
    print "  <td>" . odbc_result($result, "mach_model") . "\n";
    print "  <td>" . odbc_result($result, 'mach_video_yn') . "\n";
    print "  <td>" . odbc_result($result, "mach_video_open") . "\n";
    print "  <td>" . odbc_result($result, "mach_pic_yn") . "\n";
    print "  <td>" . odbc_result($result, 'mach_pic_open') . "\n";
    print "  <td>" . odbc_result($result, "mach_spec_yn") . "\n";
    print "  <td>" . odbc_result($result, "mach_spec_open") . "\n";
    print "</tr>\n";
}

odbc_free_result($result);
odbc_close($db);
?>
</tr>
</tbody>
</table>
</body>
</html>
UnixSan
  • 13
  • 5
  • I don't have enough experience with the `odbc` PHP extension, but in a similar situation (storing and retrieving bulgarian text data), I use the [PHP Driver for SQL Server](https://learn.microsoft.com/en-us/sql/connect/php/microsoft-php-driver-for-sql-server?view=sql-server-ver16). – Zhorov Oct 11 '22 at 05:44
  • Do you happen to know how to install it? I cannot find instructions anywhere. – UnixSan Oct 11 '22 at 05:53
  • You may try to start from [here](https://learn.microsoft.com/en-us/sql/connect/php/step-1-configure-development-environment-for-php-development?view=sql-server-ver16). – Zhorov Oct 11 '22 at 05:56
  • I configured the stuff but it still does not work – UnixSan Oct 11 '22 at 06:26
  • Can you edit the question with the current attempt (using PHP Driver for SQl Server) and additional information about the `mach_descr` column collation? – Zhorov Oct 11 '22 at 06:29
  • You are still using the `odbc` functions. You need to use `sqlsrv` functions or a PDO-based approach with `sqlsrv` driver. [This](https://stackoverflow.com/questions/58937971/change-php-mssql-to-sqlsrv-mssql-fetch-row) is something similar. – Zhorov Oct 11 '22 at 08:14
  • How shall I edit the "odbc_result"? Replace it with query_result? – UnixSan Oct 11 '22 at 08:31
  • With `... while ($row = sqlsrv_fetch_array($result, SQLSRV_FETCH_ASSOC)) {echo print_r($row, true);} ...` for example. – Zhorov Oct 11 '22 at 08:33
  • I get this error "Uncaught TypeError: sqlsrv_fetch_array(): supplied resource is not a valid ss_sqlsrv_stmt" – UnixSan Oct 11 '22 at 08:50

0 Answers0