简体   繁体   中英

Fetching MySQL Geo Point Data and storing result in PHP variable

My goal is to insert some point data into a MySQL database. I populated the database with some dummy data and it seems to work. Later, I would like to retrieve that data and store it in a PHP variable so I can post an html table of the data.

Point   X   Y
   P1     1   1
   P2     4   2

Note: Please don't hate on the "art".

I am using the following query string to retrieve the data from the MySQL database. I've used this code in non-point based data and have no problems, things run just fine.

$sql = "SELECT * FROM " . $this->tableName . " WHERE `OTHER` = '" . $Otherdata . "' AND `POSITION` = 'POINT(1,1)' LIMIT 0, 30 ";
    $query = mysqli_query($connection, $sql) or trigger_error("Query Failed: " . mysql_error());

    $row = mysqli_fetch_assoc($query);

    //print_r($row);//This is empty, or null...which means my $sql line is wrong too...

    $Index = 0;
    while ($row = mysqli_fetch_array($query, MYSQL_ASSOC)) {
        $this->PositionX[$Index] = $row['POSITION']; //Error must be HERE!                  
        $this->PositionY[$Index] = $row['POSITION']; // How do I access .X() or [0] or whatever it 
        $this->OtherVariable[$Index] = $row['OTHER']; // is that points to the first element of Point?
        $Index++;
    }

Long story short, this doesn't work. $row['POSITION'] has no idea that I want the X component. Also $row is null or empty, which means my $sql must not be correct.

Question 1:

What is the correct way of checking if a Point(x,y) is equal to some point in the DB in MySQL?

Edit: Found an answer Here . The 'POINT(1,1)' part of the query should actually just be POINT(1,1) that is, without the ' characters. The correct line reads:

$sql = "SELECT * FROM " . $this->tableName . " WHERE `OTHER` = '" . $Otherdata . "' AND `POSITION` = POINT(1,1) LIMIT 0, 30 ";

Question 2:

What is the syntax to retrieve a piece of point data from a MySQL database and store it the X and Y components, in a PHP Variable? (Technically two php variables, one for X and one for Y, but you know what I mean.)

Another way to word it, how can I call echo $row['POSITION']; (for X and Y independently)?

I ended up using the the As keyword.

"SELECT X(`POSITION`) as Xpos, Y(`POSITION`) as Ypos, `Other1`, `Other2`, `Other3`, `Other4` FROM " . $this->tableName . " WHERE `Other4` = '" . $Otherdata . ";

This let me access just one component of the Point.

$myXpos = $row['Xpos'];
$myYpos = $row['Ypos'];

Thus accessing the X and Y components of a Point data type object independently.

I've recently been dealing with the same problem and whilst the above method does work, for anybody who would like to extract the coordinates from a binary data blob (as received from 'fetch_assoc' for example), then this method should work:

function rawPointToFloatPair($data)
{   
    $res = unpack("lSRID/CByteOrder/lTypeInfo/dX/dY", $data);
    return [$res['X'],$res['Y']];
}

The data structure features 4 bytes as an ID, followed by a byte to define byte order (0 = big endian, 1 = little endian. Not handled here for brevity) and then 4 bytes for 'integer information' (where a value of '1' means this is a point). The next 16 bytes are two double precision floating point numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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