'PDOException' with message 'SQLSTATE[22001]: String data, right truncated: 0
<p><strong>NOTE:</strong> I have narrowed this problem down to specifically PDO because I am able to successfully prepare and execute statements using the <a href="http://php.net/manual/en/book.uodbc.php" rel="nofollow noreferrer">odbc_*</a> functions.</p>

<p><em>Why can't I bind this parameter to the PDO prepared statement?</em></p>

<p>This works:</p>

<pre><code>$mssqldriver = 'ODBC Driver 13 for SQL Server';
$pdoDB = new PDO("odbc:Driver=$mssqldriver;Server=$hostname;Database=$dbname", $username, $password);
$pdoDB-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION );

$sql = "SELECT 'value' AS col where 'this' = 'this'";
$stmt = $pdoDB-&gt;prepare($sql);
$params = [];
$stmt-&gt;execute($params);
print_r($stmt-&gt;fetch());
</code></pre>

<blockquote>
<pre><code>Array ( [col] =&gt; value [0] =&gt; value )
</code></pre>
</blockquote>

<p>Does not work:</p>

<pre><code>$sql = "SELECT 'value' AS col where 'this' = ?";
$stmt = $pdoDB-&gt;prepare($sql);
$params = ['this'];
$stmt-&gt;execute($params);
print_r($stmt-&gt;fetch());
</code></pre>

<p>Web Server is running PHP 5.5.9 on <strong>Linux Ubuntu 14.04</strong> with ODBC Driver 13 for SQL Server and connecting to <strong>Microsoft SQL Server 2012</strong> on Windows Server 2012</p>

<p>Here's the full error:</p>

<blockquote>
<pre><code>Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[22001]:
String data, right truncated: 0
[Microsoft][ODBC Driver 13 for SQL Server]
String data, right truncation
(SQLExecute[0] at /build/buildd/php5-5.5.9+dfsg/ext/pdo_odbc/odbc_stmt.c:254)' in /var/www/scratch.php:46
Stack trace:
#0 /var/www/scratch.php(46): PDOStatement-&gt;execute(Array)
#1 {main} thrown in /var/www/scratch.php on line 46
</code></pre>
</blockquote>

<p>I have also tried setting:</p>

<pre><code>$pdoDB-&gt;setAttribute( PDO::ATTR_EMULATE_PREPARES, true );
</code></pre>

<p>And using named parameters:</p>

<pre><code>$sql = "SELECT 'value' AS col where 'this' = :myVal";
$stmt = $pdoDB-&gt;prepare($sql);
$params = ['myVal' =&gt; 'this'];
$stmt-&gt;execute($params);
print_r($stmt-&gt;fetch());
</code></pre>

<p>Even with an explicit colon:</p>

<pre><code>$params = [':myVal' =&gt; 'this'];
</code></pre>

<p>I also tried just using <a href="http://php.net/manual/en/pdostatement.bindparam.php" rel="nofollow noreferrer"><code>bindParam</code></a> as demonstrated in <a href="https://stackoverflow.com/a/31930277/4233593">this answer</a>:</p>

<pre><code>$sql = "SELECT 'value' AS col where 'this' = ?";
$stmt = $pdoDB-&gt;prepare($sql);
$param = 'this';
$stmt-&gt;bindParam(1, $param);
$stmt-&gt;execute();
print_r($stmt-&gt;fetch());
</code></pre>

<p>As well as with named parameters:</p>

<pre><code>$sql = "SELECT 'value' AS col where 'this' = :myVal";
$stmt = $pdoDB-&gt;prepare($sql);
$param = 'this';
$stmt-&gt;bindParam(':myVal', $param, PDO::PARAM_STR);
$stmt-&gt;execute();
print_r($stmt-&gt;fetch());
</code></pre>

<p>If I try to explicitly set the length:</p>

<pre><code>$stmt-&gt;bindParam(':myVal', $param, PDO::PARAM_STR, 4);
</code></pre>

<p>I get a bonus error:</p>

<blockquote>
<pre><code>Fatal error: Uncaught exception 'PDOException' with message
'SQLSTATE[42000]: Syntax error or access violation: 102
[Microsoft][ODBC Driver 13 for SQL Server][SQL Server]
Incorrect syntax near 'OUTPUT'.
</code></pre>
</blockquote>

<p>And yes, all this is a trivialized example without tables so that you can easily reproduce it, but just to be sure, I have actually tried this with a real table.</p>

<pre class="lang-sql prettyprint-override"><code>CREATE TABLE myTable (
    id INT IDENTITY PRIMARY KEY,
    val NVARCHAR(255)
);
INSERT INTO myTable (val) VALUES ('hello world');
</code></pre>

<p>Works:</p>

<pre><code>$sql = "SELECT * FROM myTable WHERE val = 'hello world'";
$stmt = $pdoDB-&gt;prepare($sql);
$params = [];
$stmt-&gt;execute($params);
print_r($stmt-&gt;fetch());
</code></pre>

<blockquote>
<pre><code>Array ( [id] =&gt; 1 [0] =&gt; 1 [val] =&gt; hello world [1] =&gt; hello world )
</code></pre>
</blockquote>

<p>Does not work:</p>

<pre><code>$sql = "SELECT * FROM myTable WHERE val = ?";
$stmt = $pdoDB-&gt;prepare($sql);
$params = ['hello world'];
$stmt-&gt;execute($params);
print_r($stmt-&gt;fetch());
</code></pre>

<p>All paths lead to the same error:</p>

<blockquote>
  <p>String data, right truncated</p>
</blockquote>