Invalid GIS data provided to function st_geometryfromtext in MySQL - php

Here is my error:
Error Number: 3037
Invalid GIS data provided to function st_geometryfromtext.
Here is my code:
SELECT ST_Within(ST_GEOMFROMTEXT('POINT(12.971201 077.652038)'),
ST_GEOMFROMTEXT('POLYGON((13.517837674890684 76.453857421875),(13.838079936422464 77.750244140625),(14.517837674890684 79.453857421875),(13.517837674890684 76.453857421875))')) As geoFenceStatus

Note that if you want to wrap each set of coordinates with their own () like you have done in your query, you need to be using mysql 5.7.9+ the earlier versions do not support it. But that is not all, there is another error. mysql polygons have to be closed
SELECT ST_GEOMFROMTEXT('POLYGON((13.517837674890684 76.453857421875,13.838079936422464 77.750244140625,14.517837674890684 79.453857421875,13.517837674890684 76.453857421875,13.517837674890684 76.453857421875))')
And also note that (( and )) is the correct syntax

Related

Laravel 5.6, MySQL 5.7, Cannot select the new JSON type field

I am storing some data inside mysql using the JSON Type field.
When I try to select query my json data field in Laravel 5.6, I get an exception
$transactions = DB::table('transactions')
->select('transactions.uuids_json', '... other columns')
....
SQLSTATE[HY000]: General error: 2036, query exception
If I remove the uuids_json field, everything works normal. Do I have to somehow use raw statements or something?
The column uuid_json data field contains rows like this:
["1a7b29b8-5009-4266-8192-508930f2f92a", "3d52cfd5-d3c0-467f-8da1-cf81c344ad20", "cbe6e7fb-d806-49e2-8616-3c28afa012fe", "dfda9df5-2cbf-4cb8-aa54-a6dc23f73995"]
Thank you for any help!
This sounds related to https://bugs.php.net/bug.php?id=70384
Make sure your PHP install is using the mysqlnd 5.0.11 or later, which according to that bug log should include the fix.
Check php -i or phpinfo() for the version of mysqlnd you use.
If you can't upgrade to a version of the library that supports the JSON data type, a workaround is to CAST the JSON column to a string:
->select(\DB::raw("CAST(transactions.uuids_json as CHAR) as uuids_json"), ...)

SELECT with date field in WHERE clause fails via Ajax

A jquery builder (from http://querybuilder.js.org/ ) is used to let the user pick a date and further select data for a DataTables (datatables.net/ ) via a PHP function.
The DataTables and especially Ajax function looks like this:
var table = $(id).DataTable({
serverSide: true,
searching: true,
processing: true,,
ajax: {
url: "controllers/myAjax.php",
type: "POST",
data: result
}
});
The object passed as data is defined by queryBuilder and appended to my query string in the PHP script. To nail things down I pass the data as plain SQL (http://querybuilder.js.org/plugins.html#import-export). In my problem test case this is:
WHERE birthdate < '1990-01-01'
This would result in the SELECT query:
SELECT * from table_1 WHERE birthdate < '1990-01-01'
This query throws a MySQL error:
"[...] check the manual that corresponds to your MySQL
server version for the right syntax to use near '\'1990-01-01\' "
Obviously the date doesn't get escaped correctly. But when I enter exactly this query to my MySQL workbench, the server executes and returns a correct set of results. Even more, the workbench doesn't care if I use single quote (') or double quote (").
Further, I tried to manually remove those escape chars using PHP str_replace. The function then returns values, but obviously interpreted as int and breaking other queries (like equal ID). Same goes for msqli.real-escape-string (http://php.net/manual/de/mysqli.real-escape-string.php).
Another approach I tried was to change the dataType of the Ajax function a little bit - but basically I am sending form-encoded data, so the default type for this should be fine?
So why does (only) the date field get escaped in a wrong manner? Is there any rather quick fix for this, before I have to write my own PHP functions for accessing the DB?

Getting oci_execute(): ORA-01843: not a valid month invalid in php while inserting record?

I am getting oci_execute(): ORA-01843: not a valid month invalid. Why it is giving it?
INSERT INTO DETAIL (PPD_ID, PPD_ID,PPD_ENTRY_DATE, PPD_IGM_ID, PPD_DFM_ID, PPD_DRM_ID, PPD_HEIGHT, PPD_UOM_ID, PPD_FTM_ID, PPD_FRQ_ID, PPD_START_DATE_TIME, PPD_END_DATE_TIME, PPD_STATUS, PPD_STM_ID)
VALUES ('WS00318229',2440900,'29-12-2015',350,1,1,50,46,1,1,'29-12-2015','29-12-2015','PRESCRIBED',6)
When i am using the above query from php to insert record in to oracle database it is giving following error.
oci_execute(): ORA-01843: not a valid month invalid
But when i use the following query it is not giving any error and it is inserting record.How to solve this? This error was already is there but i am not clear about the answers.Any ideas why it is like this.
INSERT INTO DETAIL (PPD_ID, PPD_ID,PPD_ENTRY_DATE, PPD_IGM_ID, PPD_DFM_ID, PPD_DRM_ID, PPD_HEIGHT, PPD_UOM_ID, PPD_FTM_ID, PPD_FRQ_ID, PPD_START_DATE_TIME, PPD_END_DATE_TIME, PPD_STATUS, PPD_STM_ID)
VALUES ('WS00318229',2440900,'29-DEC-2015',350,1,1,50,46,1,1,'29-DEC-2015','29-DEC-2015','PRESCRIBED',6)
You are inserting strings into DATE columns. When you do that, oracle performs implicit conversion based on your NLS settings, specifically, nls_date_format. Evidently when you connect from php script and from some client you use (SQL*Plus, SQL Developer, what have you), your NLS settings differ.
You can check session parameters using following query.
SELECT * FROM nls_session_parameters;
For date presented in format '29-DEC-2015' nls_date_format should be 'DD-MON-YYYY'. If it has some other value, you can change it using following query.
alter session set nls_date_format='DD-MON-YYYY'
Or you can explicitly convert string to date using TO_DATE function and passing format as second parameter, but this will require your application code to be consistent in using only specified date format.
TO_DATE('29-DEC-2015', 'DD-MON-YYYY')

Problematic DateTime Comparison SQL Query Not Working

I have a problem with executing ODBC query. I am trying to achieve something simple.
SELECT t.* FROM table t WHERE DateTime > '#2014-05-05 00:00:00#'
I have tried to execute with or without hashes (#) and quotes around the datetime, but no success with that. Also, instead of using >, < and other comparison operators I have tried to use BETWEEN.
In addition several formats "dd/mm/yyyy", "mm/dd/yyyy", "dd.mm.yyyy", etc. plus standard sql format (iso) were tested and the result is the same.
The approach looks like:
$sql = "SELECT t.* FROM table t WHERE DateTime > '#...#'";
$result = odbc_fetch_array(odbc_exec($odbc_connection, $sql));
After this execution I receive some 5-10 symbol binary error, but no error message (with odb_error and odbc_errormsg), in addition to the:
No tuples available at this result index
(The topic: ODBC error in PHP: “No tuples available at this result index” is not helping)
Could it be some ODBC issue (old version etc.) or we should cast another black magic through odbc_exec?
Maybe ODBC have trouble with reserved word datetime.
I don't know how to escape quotes in PHP, try
SELECT t.* FROM table t WHERE t."DateTime" > '20140505'";
You should use ODBC syntax for datetimes which is { ts '1998-05-02 01:23:56.123' }. You can omit the partial seconds. See here.
Have you tried using the TO_DATE() function? You can specify the format you are using.
SELECT t.* FROM table t WHERE DateTime > TO_DATE('2014-05-05 00:00:00','YYYY-MM-DD HH24:MI:SS')

Date Insertion Issue in Oracle Database and Codeigniter

I am having a problem in updating oracle db table with date column . I have tried many solutions but still not working. I can not insert the date into the database using codeigniter active record or normal query.. the column type is Timestamp. here are some solutions I have tried. could u help please.. thank you
$this->db->set('AC_START',"TO_DATE('2014-03-4 2:30','yyyy-mm-dd hh:mm')");
$this->db->set('AC_END',"TO_DATE('2014-05-5 2:34' ,'yyyy-mm-dd hh:mm')");
$this->db->insert('ACTIVITES');
here is error message that I always get
Error Number:
INSERT INTO "LI_PPPA"."ACTIVITES" ("AC_START", "AC_END") VALUES ('TO_DATE('2014-03-4 2:30','yyyy-mm-dd hh:mm')', 'TO_DATE('2014-05-5 2:34' ,'yyyy-mm-dd hh:mm')')
Filename: Directory\program\system\database\DB_driver.php
Line Number: 330
You have to make sure CI doens't escape the values you are passing to the oracle database, because to_date is an oracle function.
Try the following:
$this->db->set('AC_START',"TO_DATE('2014-03-4 2:30','yyyy-mm-dd hh:mm')",FALSE);
$this->db->set('AC_END',"TO_DATE('2014-05-5 2:34' ,'yyyy-mm-dd hh:mm')",FALSE);
$this->db->insert('ACTIVITES');code here
With that last parameter you are telling CodeIgniter to NOT escape this value.
Sonaryr's answer threw an Oracle error for me: ORA-01810
Oracle SQL Developer Statement Output:
Error report:
SQL Error: ORA-01810: format code appears twice
01810. 00000 - "format code appears twice"
*Cause:
*Action:
To Fix Change MM to MI:
$this->db->set('AC_START',"TO_DATE('2015-09-02 16:35' ,'YYYY-MM-DD HH24:MI')",FALSE);
Write Normal Query To Perform Action Against The TO_DATE(); Codegniter Does not have any library functions for to_date() for oracle.

Categories