title: Using PHP's odbc to grab a varchar(max) column
body: <p>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.</p>
  
  <p>The field in question, called <code>description</code>, is a <code>varchar(MAX)</code> field; a stored procedure queries the database for a tuple and dumps the values from its table into text boxes on the page; the <code>description</code> field is output to a <code>textarea</code> control.</p>
  
  <p>Here is the PHP that handles pulling the information from the database:</p>
  
  <pre><code>$res = odbc_exec($dbhandle, "exec dbo.usp_ProgramGet " . $_GET["program"]);
  $id = $_GET["program"];
  $name = odbc_result($res, "title");
  $desc = odbc_result($res, "description");
  </code></pre>
  
  <p>The <code>$name</code> variable works as expected (in the database, it is of type <code>char(15)</code>). However, if (for example) the <code>description</code> field contains "This is a test" then <code>$desc</code> will result in "�$ime�����", which is what gets dumped into the <code>textarea</code> control, instead of what's stored in the database.</p>
  
  <p>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.</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>I am using SQL Server queries to update the varchar values. I tried putting in a really long string and I got this:</p>
  
  <pre><code>�,ime�������stringDayToInt��É������à‰,���N={���������������������������������������������
  </code></pre>
  
  <p>"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.</p>