简体   繁体   English

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

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

I have a table in MySQL that stores polygons.我在 MySQL 中有一个存储多边形的表。 I can read this back on the command line using the following query:我可以使用以下查询在命令行上读回:

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)

When I try to read this in PHP using the same query, polygonid comes back correctly, but thepolygon comes back as empty:当我尝试使用相同的查询在 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']);

    [...]

results in结果是

string(1) "1" NULL

meaning that 'thepolygon' comes back as NULL, but the 'polygonid' comes back just fine.意味着 'thepolygon' 返回为 NULL,但 'polygonid' 返回就好了。

If I change the query to如果我将查询更改为

SELECT polygonid, thepolygon FROM polygons

then I do get back binary data:然后我取回二进制数据:

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

It's almost as if astext() does not work.就好像 astext() 不起作用一样。 What am I doing wrong?我究竟做错了什么?

Thanks for any input at all!感谢您提供任何意见!

Looks like it might just be because you've not given the AsText() selection an alias which can be picked up from the PHP array.看起来这可能只是因为您没有给AsText()选择一个可以从 PHP 数组中提取的别名。

If you print out $row you might be able to see that your array does not have a thepolygon key.如果您打印出$row您可能会看到您的数组没有thepolygon键。

Have you tried this?你试过这个吗?

$query = "SELECT polygonid, AsText(thepolygon) AS thepolygon FROM polygons";

It works on the command line because you're just printing out whatever is selected in the query, but in PHP you're trying to print out array keys - ie the name of the fields selected.它在命令行上工作,因为您只是打印出查询中选择的任何内容,但在 PHP 中您正在尝试打印出数组键 - 即所选字段的名称。 Your MySQL query does not select a field called thepolygon , so it doesn't exist in the array either.您的 MySQL 查询没有选择名为thepolygon的字段,因此它也不存在于数组中。

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

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