我在 MySQL 中有一个存储多边形的表。我可以使用以下查询在命令行上读回:
mysql> SELECT polygonid, AsText(thepolygon) FROM polygons;
+-----------+----------------------------------------------------------------------------------------------------------------+ | polygonid | AsText(thepolygon) |
+-----------+----------------------------------------------------------------------------------------------------------------+ | 1 | POLYGON((36.96318 127.002881,37.96318 127.002881,37.96318
128.002881,36.96318 128.002881,36.96318 127.002881)) | +-----------+----------------------------------------------------------------------------------------------------------------+ 1 row in set, 1 warning (0.02 sec)
当我尝试使用相同的查询在 PHP 中读取它时,polygonid 正确返回,但 thepolygon 返回为空:
$query = "SELECT polygonid, AsText(thepolygon) FROM polygons";
$result = mysqli_query($con, $query);
while ($row = mysqli_fetch_array($result)) {
var_dump($row['polygonid']);
var_dump($row['thepolygon']);
[...]
结果是
string(1) "1" NULL
意味着 'thepolygon' 返回为 NULL,但 'polygonid' 返回就好了。
如果我将查询更改为
SELECT polygonid, thepolygon FROM polygons
然后我取回二进制数据:
string(1) "1" string(97)
"�t{I{B@�1�3/�_@�t{I�B@�1�3/�_@�t{I�B@��`@�t{I{B@��`@�t{I{B@�1�3/�_@"
string
就好像 astext() 不起作用一样。我究竟做错了什么?
感谢您提供任何意见!
看起来这可能只是因为您没有为AsText()
选择指定一个可以从 PHP 数组中提取的别名。
如果您打印出来,$row
您可能会看到您的阵列没有thepolygon
密钥。
你试过这个吗?
$query = "SELECT polygonid, AsText(thepolygon) AS thepolygon FROM polygons";
它在命令行上工作,因为您只是打印出查询中选择的任何内容,但在 PHP 中您正在尝试打印出数组键 - 即所选字段的名称。您的 MySQL 查询未选择名为 的字段thepolygon
,因此它也不存在于数组中。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句