如何使用 PHP 从 MySQL 数据库读取几何数据类型
How to read geometry data type from MySQL database using PHP
我在 MySQL 中有一个存储多边形的 table。我可以使用以下查询在命令行上读回:
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 正确返回,但返回的多边形为空:
$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()
selection 一个可以从 PHP 数组中获取的别名。
如果您打印出 $row
,您可能会看到您的数组没有 thepolygon
键。
你试过吗?
$query = "SELECT polygonid, AsText(thepolygon) AS thepolygon FROM polygons";
它在命令行上工作,因为你只是打印出查询中 selected 的任何内容,但在 PHP 中你试图打印出数组键 - 即名称字段 selected。您的 MySQL 查询没有 select 一个名为 thepolygon
的字段,因此它也不存在于数组中。
我在 MySQL 中有一个存储多边形的 table。我可以使用以下查询在命令行上读回:
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 正确返回,但返回的多边形为空:
$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()
selection 一个可以从 PHP 数组中获取的别名。
如果您打印出 $row
,您可能会看到您的数组没有 thepolygon
键。
你试过吗?
$query = "SELECT polygonid, AsText(thepolygon) AS thepolygon FROM polygons";
它在命令行上工作,因为你只是打印出查询中 selected 的任何内容,但在 PHP 中你试图打印出数组键 - 即名称字段 selected。您的 MySQL 查询没有 select 一个名为 thepolygon
的字段,因此它也不存在于数组中。