Using PHP odbc to grab a varchar (max) column

I am building a PHP site for a class project, and we use the MS SQL Server 2008 database to populate the fields on the site. However, one of the fields displays garbage on the page instead of what is actually stored in the database.

The field in question is called descriptiona field varchar(MAX); the stored procedure queries the database for the tuple and uploads the values ​​from its table into the text fields on the page; the field descriptionis displayed on the control textarea.

Here is the PHP that handles extracting information from a database:

$res = odbc_exec($dbhandle, "exec dbo.usp_ProgramGet " . $_GET["program"]);
$id = $_GET["program"];
$name = odbc_result($res, "title");
$desc = odbc_result($res, "description");

The variable $nameworks as expected (in the database it is of type char(15)). However, if (for example) the field descriptioncontains "This is a test", then it $descwill lead to "$ ime", and this is what is then dumped into the control textareaof what is stored in the database.

I searched everything and did not find solutions to this problem, although it sounds like an error in PHP itself, although I'm not sure.


Update

I am using SQL Server queries to update varchar values. I tried to insert a very long string, and I got the following:

 ,ime       stringDayToInt  É      à‰,   N={                                             

"stringDayToInt" is the name of the PHP function that I wrote, which lives in a completely different file, which ended up on the page I'm trying to execute. Very strange.

+3
source share
5 answers

: php /

FFFD

, Unicode

UPDATE:

:

echo mb_detect_encoding($desc).' ';
var_dump($desc);

mb_detect_encoding

if(is_utf8($desc)) {
    echo "UTF-8 Encoded";
} else {
    echo "Oops, not UTF-8";
}

var_dump($desc);

function is_utf8($str) {
    $c=0; $b=0;
    $bits=0;
    $len=strlen($str);
    for($i=0; $i<$len; $i++){
        $c=ord($str[$i]);
        if($c > 128){
            if(($c >= 254)) return false;
            elseif($c >= 252) $bits=6;
            elseif($c >= 248) $bits=5;
            elseif($c >= 240) $bits=4;
            elseif($c >= 224) $bits=3;
            elseif($c >= 192) $bits=2;
            else return false;
            if(($i+$bits) > $len) return false;
            while($bits > 1){
                $i++;
                $b=ord($str[$i]);
                if($b < 128 || $b > 191) return false;
                $bits--;
            }
        }
    }
    return true;
}
0

Ugly PHP 5.3...

.php , .
ramdom script php-. .

SQL Server: MS SQL Server 2008 R2

odbc: Driver = { SQL Server 10.0}

PHP: PHP 5.3

, .
, , :

select cast(the_column_varcharmax as text) as column_name from table_name

, , .

+3

MSSQL/ODBC/PHP , , ​​ . :

select name, convert(text,description) as description. .....

.

+2

bug PHP.

.

+1

PHP 5.3 MS SQL 2008 R2. , ( ), ODBC SQLSRV, , , .

0

Source: https://habr.com/ru/post/1791243/


All Articles