Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I do a Parameterized Query containing Geometry Function?

I have an SQL query that goes like this:

$stmt = $dbh->prepare("INSERT INTO Places (name, latlng)
 VALUES (?, GeomFromText('POINT(? ?)'))");
$stmt->bindValue(1, $_POST['name']);
$stmt->bindValue(2, $_POST['lat']);
$stmt->bindValue(3, $_POST['lng']);
$stmt->execute();

I have been getting this error:

'PDOException' with message 'SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'latlng' cannot be null'

The latlng column is of type point NOT NULL. Since the insert works for the following, why would it cause a NULL to be inserted for the above?

"INSERT INTO Places (name, latlng)
 VALUES (?, GeomFromText('POINT(".$_POST['lat']." ".$_POST['lng'].")'))"

So, my question is, can I do a parameterized query containing a geometry function? If so, how? If not, why?

like image 903
Question Overflow Avatar asked Dec 02 '11 10:12

Question Overflow


1 Answers

In your query POINT(? ?) is just a string, not a function. You can't just parametrize part of a string, you need to parametrize the whole string:

$stmt = dbh->prepare("INSERT INTO Places (name, latlng) VALUES (?, GeomFromText(?))");
$stmt->bindValue(1, $_POST['name']);
$stmt->bindValue(2, 'POINT('.(float)$_POST['lat'].' '.(float)$_POST['lng'].')');
like image 72
XzKto Avatar answered Sep 21 '22 14:09

XzKto