I have a query:
SELECT * FROM Table
Which returns the following results:
ID | Description |
---|---|
1 | 1234 |
2 | 12345 |
3 | 12— |
4 | 123— |
5 | 1234 |
In the table above, the Description field is defined as Description VARCHAR(5)
. I believe that my issue resides with the em dash —
in that field. This issue also occurs with the en dash –
but not a hyphen -
.
The actual issue is occurring when I get to the PHP side of things. When I echo
out the table returned by sqlsrv_query
it will not print any fields after ID #3. I cannot figure out why it stops printing those results.
Here is the simplified code:
<?php
//Connect to the database
include_once("config.php");
//Execute the stored procedure
$query = "EXEC sp_query";
$stmt = sqlsrv_query($conn, $query);
?>
<!DOCTYPE HTML>
<head>
<meta charset="utf-8">
</head>
<body>
<table>
<thead>
<th>ID</th>
<th>Description</th>
</thead>
<?php
//Loop through the current array
while($row = sqlsrv_fetch_array($stmt, SQLSRV_FETCH_ASSOC)) {
echo "<tr>";
echo "<td>".$row['ID']."</td>";
echo "<td>".$row['Description']."</td>";
echo "</tr>";
}
//Release the result resource
sqlsrv_free_stmt($stmt);
?>
</table>
</body>
</html>
Loading this web page returns the table:
ID | Description |
---|---|
1 | 1234 |
2 | 12345 |
3 | 12— |
If I remove the one em dash from ID #4 then it will return the same exact results as the TSQL query shown at the top of this question. Is PHP somehow thinking that the max field length has been exceeded with the use of the em dash? Does anyone now how I can remedy this?
Things I have tried:
- Using different charsets
CAST
ing the Description field toVARCHAR(MAX)
in the stored procedure- Selecting only the first 2 characters from the field
LEFT(Description, 2)
- Removing the emdash before the
echo
using PHP functionstr_replace
None of these solved my issue. It may also be worth noting that the sqlsrv_errors
function does not return any errors for the stored procedure.