sqlsrv_query 如果字段的最大长度已满,则停止使用 em dash 回显结果
sqlsrv_query stops echoing results with em dash if max length of field is filled
我有一个问题:
SELECT * FROM Table
其中 return 结果如下:
ID
Description
1
1234
2
12345
3
12—
4
123—
5
1234
在上面的 table 中,Description 字段定义为 Description VARCHAR(5)
。我认为我的问题在于该字段中的短划线 —
。破折号 –
但连字符 -
.
也会出现此问题
真正的问题发生在我到达 PHP 的时候。当我 echo
输出由 sqlsrv_query
编辑的 table return 时,它不会打印 ID #3 之后的任何字段。我不明白为什么它停止打印这些结果。
这里是简化代码:
<?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>
正在加载此网页 returns the table:
ID
Description
1
1234
2
12345
3
12—
如果我从 ID #4 中删除一个 em 破折号,那么它将 return 与此问题顶部显示的 TSQL 查询完全相同的结果。 PHP 是否以某种方式认为使用 em 破折号已超过最大字段长度?现在有人知道我该如何补救吗?
我尝试过的事情:
- 使用不同的字符集
CAST
在存储过程 中将描述字段设为VARCHAR(MAX)
- 仅选择字段中的前 2 个字符
LEFT(Description, 2)
- 使用 PHP 函数
str_replace
删除 echo
之前的破折号
其中 None 解决了我的问题。可能还值得注意的是,sqlsrv_errors
函数不会 return 存储过程的任何错误。
事实证明,我在数据库连接信息中缺少 CharacterSet
参数;添加解决了我的问题。我错误地认为这是由 HTML head
的 meta
标签定义的:<meta charset="utf-8">
$connectionInfo = array(
"Database"=>"dbName",
"UID"=>"username",
"PWD"=>"password",
"CharacterSet" => "UTF-8" // <-- Added this line to existing connection info
);
非常感谢@miken32 为我指明了正确的方向!
我有一个问题:
SELECT * FROM Table
其中 return 结果如下:
ID | Description |
---|---|
1 | 1234 |
2 | 12345 |
3 | 12— |
4 | 123— |
5 | 1234 |
在上面的 table 中,Description 字段定义为 Description VARCHAR(5)
。我认为我的问题在于该字段中的短划线 —
。破折号 –
但连字符 -
.
真正的问题发生在我到达 PHP 的时候。当我 echo
输出由 sqlsrv_query
编辑的 table return 时,它不会打印 ID #3 之后的任何字段。我不明白为什么它停止打印这些结果。
这里是简化代码:
<?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>
正在加载此网页 returns the table:
ID | Description |
---|---|
1 | 1234 |
2 | 12345 |
3 | 12— |
如果我从 ID #4 中删除一个 em 破折号,那么它将 return 与此问题顶部显示的 TSQL 查询完全相同的结果。 PHP 是否以某种方式认为使用 em 破折号已超过最大字段长度?现在有人知道我该如何补救吗?
我尝试过的事情:
- 使用不同的字符集
CAST
在存储过程 中将描述字段设为- 仅选择字段中的前 2 个字符
LEFT(Description, 2)
- 使用 PHP 函数
str_replace
删除
VARCHAR(MAX)
echo
之前的破折号
None 解决了我的问题。可能还值得注意的是,sqlsrv_errors
函数不会 return 存储过程的任何错误。
事实证明,我在数据库连接信息中缺少 CharacterSet
参数;添加解决了我的问题。我错误地认为这是由 HTML head
的 meta
标签定义的:<meta charset="utf-8">
$connectionInfo = array(
"Database"=>"dbName",
"UID"=>"username",
"PWD"=>"password",
"CharacterSet" => "UTF-8" // <-- Added this line to existing connection info
);
非常感谢@miken32 为我指明了正确的方向!