繁体   English   中英

如何使用 PHP 从 MySQL 数据库中读取几何数据类型

[英]How to read geometry data type from MySQL database using PHP

我在 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的字段,因此它也不存在于数组中。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM