How to insert point data into mysql using PDO bindParam?

TryHarder

DETAILS

I'd like to make use of mysql's spatial extension, so I am trying to store longitude and latitude in a mysql table of datatype POINT using bindParam. Unfortunately, I keep getting the error SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'location' cannot be null.

I've checked that longitude and latitude have values. So the problem has to be with my code, but I cannot see what I am doing wrong.

Here is the code I am using.

$location=$latitude." ".$longitude;
$sql = "INSERT INTO my_geodata SET location = PointFromText('POINT(:location)')";
      //INSERT INTO my_geodata SET location = PointFromText('POINT(-41 12)');    

try 
{
    $stmt = $dbh->prepare($sql);            
    $stmt->bindParam(':location', $location, PDO::PARAM_STR);           
    $stmt->execute();   
    $dbh = null;
}

catch(PDOException $e)
{               
    echo $error=$e->getMessage();
}

QUESTION

What am I doing wrong? How can I insert longitude and latitude into a mysql table (that uses POINT datatype) with PDO and bindParam?

VARIATION

Based on AgreeOrNot's answer, a slightly different way to achieve this is

$location = 'POINT(' . $latitude . " " . $longitude . ')';    
$sql = "INSERT INTO my_geodata (location) VALUES (PointFromText(:location))";
Inglis Baderson

Note that parameterizing queries is not (simple) string replacement. In your code your query parameter is put in a string literal which will be kept untouched.

Try this:

$location = 'POINT(' . $latitude . " " . $longitude . ')';
$sql = "INSERT INTO my_geodata SET location = PointFromText(:location)";

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

PDO insert BindParam mysql

From Dev

How to insert compressed data using PDO?

From Dev

PHP PDO bindParam() and MySQL BIT

From Dev

Inserting data to mysql database using PDO with direct insert statement

From Dev

Inserting data to mysql database using PDO with direct insert statement

From Dev

INSERT into DB table with PDO prepare and bindParam

From Dev

PHP PDO - INSERT INTO with bindParam does not work

From Dev

Using mysql insert with implode in PDO

From Dev

How to insert unknown amount of colums in mysql using PHP PDO

From Dev

how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

From Dev

How to insert form data into PDO using prepared statements?

From Dev

Only show PDO BindParam when data is posted

From Dev

Insert JSON decoded data into mysql with PDO

From Dev

How to insert data into MySql using an associative array

From Dev

How to insert data to mysql using php

From Dev

PDO Insert operation using PHP and MYSQL not working

From Dev

Unable to insert into table using PDO in mysql

From Dev

Insert data with associative array using PDO

From Dev

How to tell if PDO MySQL INSERT was successful with IGNORE

From Dev

get first insert id for multiple insert using pdo in mysql

From Dev

How to escape a string to insert into MySQL via PDO *without* using prepared statements (legacy app)

From Dev

PHP PDO using bindParam first argument without colon

From Dev

Cant execute my PDO query using "execute()" getting "bindParam()" errors

From Dev

Converting MySql Insert To PDO

From Dev

INSERT INTO array with MySQL and PDO

From Dev

Insert data AJAX using PHP not working using PDO

From Dev

How to insert into database using PDO where columns to insert change?

From Dev

How to get the insert ID after insert using the PDO transactino in php

From Dev

Inserting Data to MySQL using PDO for PHP Xcode

Related Related

  1. 1

    PDO insert BindParam mysql

  2. 2

    How to insert compressed data using PDO?

  3. 3

    PHP PDO bindParam() and MySQL BIT

  4. 4

    Inserting data to mysql database using PDO with direct insert statement

  5. 5

    Inserting data to mysql database using PDO with direct insert statement

  6. 6

    INSERT into DB table with PDO prepare and bindParam

  7. 7

    PHP PDO - INSERT INTO with bindParam does not work

  8. 8

    Using mysql insert with implode in PDO

  9. 9

    How to insert unknown amount of colums in mysql using PHP PDO

  10. 10

    how to Insert multiple arrays with multiple rows into MySQL using PHP PDO

  11. 11

    How to insert form data into PDO using prepared statements?

  12. 12

    Only show PDO BindParam when data is posted

  13. 13

    Insert JSON decoded data into mysql with PDO

  14. 14

    How to insert data into MySql using an associative array

  15. 15

    How to insert data to mysql using php

  16. 16

    PDO Insert operation using PHP and MYSQL not working

  17. 17

    Unable to insert into table using PDO in mysql

  18. 18

    Insert data with associative array using PDO

  19. 19

    How to tell if PDO MySQL INSERT was successful with IGNORE

  20. 20

    get first insert id for multiple insert using pdo in mysql

  21. 21

    How to escape a string to insert into MySQL via PDO *without* using prepared statements (legacy app)

  22. 22

    PHP PDO using bindParam first argument without colon

  23. 23

    Cant execute my PDO query using "execute()" getting "bindParam()" errors

  24. 24

    Converting MySql Insert To PDO

  25. 25

    INSERT INTO array with MySQL and PDO

  26. 26

    Insert data AJAX using PHP not working using PDO

  27. 27

    How to insert into database using PDO where columns to insert change?

  28. 28

    How to get the insert ID after insert using the PDO transactino in php

  29. 29

    Inserting Data to MySQL using PDO for PHP Xcode

HotTag

Archive