Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using PHP's odbc to grab a varchar(max) column

I'm putting together a PHP website for a class project, and we are using a MS SQL Server 2008 database to populate fields on the site. However, one of the fields is outputting garbage onto the page instead of what is actually stored in the database.

The field in question, called description, is a varchar(MAX) field; a stored procedure queries the database for a tuple and dumps the values from its table into text boxes on the page; the description field is output to a textarea control.

Here is the PHP that handles pulling the information from the 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 $name variable works as expected (in the database, it is of type char(15)). However, if (for example) the description field contains "This is a test" then $desc will result in "�$ime�����", which is what gets dumped into the textarea control, instead of what's stored in the database.

I've searched all over and found no solutions to this problem yet, although it sounds like a bug in PHP itself although I'm not sure.


Update

I am using SQL Server queries to update the varchar values. I tried putting in a really long string and I got this:

�,ime�������stringDayToInt��É������à‰,���N={���������������������������������������������

"stringDayToInt" is the name of a PHP function I wrote that lives in a totally different file that got included into the page I'm trying out. Very bizarre.

like image 951
Andrew Avatar asked Sep 17 '25 15:09

Andrew


2 Answers

I had the same Ugly problem on PHP 5.3...

The .php page displayed random characters where must be the column value.
Some times was ramdom characters and script sections of executed php page. This is not good.

SQL Server: MS SQL Server 2008 R2

My odbc connect driver: Driver={SQL Server Native Client 10.0}

My PHP version: PHP 5.3

I was looking around but I did not find the solution.
The workaround I tried and work for me is:

select cast(the_column_varcharmax as text) as column_name from table_name

Try it, I hope it works for you.

like image 149
Saak Canales M. Avatar answered Sep 20 '25 07:09

Saak Canales M.


This bug is in MSSQL/ODBC/PHP for long time and I lost the hope it will be fixed in my life. The solution is converting it to text in the procedure:

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

I do it so for many years.

like image 36
Hink Avatar answered Sep 20 '25 05:09

Hink