I need to insert a POINT value into my MySQL table using Fat Free Framework. I was hoping to do this using the F3 Mapper, but I got the impression that is not possible.
So I tried to use $db-exec()
This is my current code, based on various searches here and on google.
$geopoint = "POINT($lat $long)";
$db->exec("INSERT INTO event_dates ('eventGeoPoint') VALUES ($geopoint)");
This throws an error:
PDOStatement: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '52.8583742))' at line 1
I have also tried
$db->exec("INSERT INTO event_dates ('eventGeoPoint') VALUES (GeomFromText($geopoint))");
Please tell me how to correctly insert a POINT() value into my database using Fat Free Framework, either the mapper or exec
You need to correct your INSERT statement, which you are mixed with UPDATE statement.
INSERT INTO mytable SET myGeoPoint = 'GeomFromText($geopoint)
should be
INSERT INTO mytable(myGeoPoint) values (GeomFromText($geopoint))
Also, you need to use Prepared Statement to avoid SQL Injection.
Searching on after #Ravi's comments I found the answer in this post.
I changed my statement to
$result= $db->exec("INSERT INTO mytable (GeoPoint) VALUES (PointFromText(CONCAT('POINT(',$lat,' ',$long,')')))");
And it works!
Related
i just switched over from a Mysql server to SQL server. But i just found out that INSERT INGORE INTO doesn't work with sql server.
Original code:
INSERT IGNORE INTO DATA_EXACT_INVENTORY_LOCATIONS (ID, Code, Opslaglocatie, Omschrijving, OpVoorraad)
VALUES ('$inventorylocationID','$inventorylocationsItemCode','$inventoryStorageLocationsCode','$inventorylocationsItemDescription','$inventorylocationsCurrenctStock')
I found out that i can use on duplicate key update, but the problem is that i have sql query's with upto 50 variables. So to use on duplicate key update would be alot of work. So what i was wondering is there a better alternative for INSERT IGNORE INTO that's is just plug and play so i don't have to write all variables again.
You can use not exists:
INSERT DATA_EXACT_INVENTORY_LOCATIONS (ID, Code, Opslaglocatie, Omschrijving, OpVoorraad)
SELECT ID, Code, Opslaglocatie, Omschrijving, OpVoorraad
FROM (VALUES ('$inventorylocationID', '$inventorylocationsItemCode', '$inventoryStorageLocationsCode', '$inventorylocationsItemDescription', '$inventorylocationsCurrenctStock')
) V(ID, Code, Opslaglocatie, Omschrijving, OpVoorraad)
WHERE NOT EXISTS (SELECT 1
FROM DATA_EXACT_INVENTORY_LOCATIONS deil
WHERE deil.id = v.id -- or whatever column gets the duplicate key
);
Alternatively, you could rewrite the code to use MERGE. The SELECT should work in both databases.
Let me also add that you should learn to use parameters. Munging query strings with constant values exposes your code to SQL injection attacks and to hard-to-debug syntax errors.
I'm learning PHP and MySQL and made a small website to help me learn. While I was implementing MySQLi, I kept getting an error with the code. I tried various things like trying to query all the tables contents but I get 0 rows even though I have a few already there. I'm using WAMP Server 2.5 and The connection is successful but anything else seems to not work.
When I use the code:
$sql = "USE mydb
INSERT INTO persons (Name, User, Pass)
VALUES ('John', 'Doe', 'johnexample');";
if ($conn->query($sql) === TRUE){
echo "Success";
}else{
echo "Error, Please Try Again Later<br>".$conn->error;
}
$conn->close();
I'm surprised this doesnt work because it's almost the exact same as the example on the W3 Schools webiste. I've seen other posts with this error but their solutions dont work. The error I get is:
You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'INSERT INTO
persons (Name, User, Pass) VALUES ('John', 'Doe', 'johnexample')' at line 2
Any help is appreciated! Thank You!
The issue is mysqli::query doesn't support multiple queries, and even if it did, it would require the ; terminator. This is causing your SQL syntax error.
If you want to change the current database for the connection, you can use:
$conn->select_db('mydb');
Then you can do your INSERT query without the USE part, which will be applied to mydb.
Be aware that the fieldnames are case-sensitive! please check if Name, User, Pass are really start with a capital.
I am using a readymade script to backup my MySQL database using PHP. I store the resultant query in a variable.
If I echo the variable, and copy paste the output into the MySQL console, it works perfectly.
But when I run the same using 'mysql_query' (I know it is depreciated, kindly ignore that), I get the dreaded Syntax error.
Here's the echo output (first 2 lines) :
INSERT INTO assign
VALUES('75085','rsam','CE0001/CZ0001/CPE183/CSC183','1','1','3.0','13','1','1','13','2','10.00','117.00','0','0');INSERT
INTO assign
VALUES('75086','rsam','CE0001/CZ0001/CPE183/CSC183','1','2','3.0','13','1','1','13','2','10.00','97.50','0','0');
And here's the exact error :
You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax to use
near 'INSERT INTO assign
VALUES('75085','rsam','CE0001/CZ0001/CPE183/CSC183','1','1'' at line 1
If anyone can point out what I am obviously missing, I would be grateful!
As the documentation for mysql_query() says:
mysql_query() sends a unique query (multiple queries are not supported) to the currently active database on the server that's associated with the specified link_identifier.
You might be interested in mysql_multi_query():
Executes one or multiple queries which are concatenated by a semicolon.
While mysql_query is limited to a single statement, this situation can be avoided as multiple records can be inserted into the same table with only one statement:
INSERT INTO assign (...)
VALUES(...),
VALUES(...);
This will save on round-trip latency (over multiple mysql_query) which might matter.
See Inserting multiple rows in mysql
Okay, so I'm currently using mysqli_real_escape_string to escape my SQL queries before sending them to MySQL via PHP. Yet, for some reason my queries aren't processing, and when I outputted the MySQL query and pasted it in to PHPMyAdmin, it gave the following error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE ind={A$RTkAIqah0J1N$Fqymnud9s5PwnWw2wC.Y02oDo4H3W8QJPoJ$6$KK8UearuUCDH$FQg' at line 1
Now, the following is my query:
INSERT INTO `db`.table(`colheader`) VALUES ('{\"hey\":[\"Hello world\",\"7\\/9\\/2013\"]}') WHERE ind='$6$RTkAIqah0J1N$Fqymnud9s5PwnWw2wC.Y02oDo4H3W8QJPoJ$6$KK8UearuUCDH$FQgSnLHIlkBOtDTzu9AuZIZTr6GS4Rzr.iW11041994'
Now, I know that the string assigned to 'ind' has some issues, but I tried putting a slash before every period and every dollar sign and it still doesn't work. I tried putting the whole thing in double quotes, even brackets. Nothing. Could anyone point out what I'm clearly missing? I've looked at the documentation and can't seem to find anything. Thank you in advance!!
WHERE serves to filter which records will be affected or retrieved by your query, and INSERT servers to append a whole new record to a table.
An INSERT can never affect existing records, therefore its nonsense to have a WHERE clause. INSERT does not support WHERE.
If you are trying to edit the value of a field on an existing record, use UPDATE instead.
Take a look at the MySQL Reference Manual for details about its usage.
if your trying to make an update to the specified index use
UPDATE `db`.table SET `colheader` = '{\"hey\":[\"Hello world\",\"7\\/9\\/2013\"]}' WHERE ind='$6$RTkAIqah0J1N$Fqymnud9s5PwnWw2wC.Y02oDo4H3W8QJPoJ$6$KK8UearuUCDH$FQgSnLHIlkBOtDTzu9AuZIZTr6GS4Rzr.iW11041994'
Someone told me that when you are working with PDO, you cannot use "INSERT INTO .... SET" to insert data into database, because it will not work on databases other than MySQL. I'm not sure what exactly he means, maybe he means I should use the other method of inserting like,
INSERT INTO table (column1, column2) VALUES (?, ?)
I tried searching on the internet for this, but I couldn't find anything. Please let me know about this.
Thank you.
You should use the INSERT INTO table (column1, column2) VALUES (?, ?) statement instead of the INSERT INTO table SET column1=? statement, because that's the correct syntax for SQL based database languages. Although MySQL accepts it, others may not.
The INSERT INTO ... SET syntax is not part of the ANSI SQL standard and therefore is not supported as widely across different RDBMS implementations. If you are designing your application such that it is tightly coupled to MySQL, using this syntax would be OK. If you are trying to design such that your application is not tightly coupled with the RDBMS implementation, then you should use the more standard INSERT INTO table (columns) VALUES (values) syntax.
Positional parameters in PHP PDO is just fine. The other option is named parameters. If I remember correctly from what I've seen in the PDO C code, it is PHP and not the DBM that does the replacements.
http://php.net/manual/en/pdo.prepared-statements.php
As stated in this the only metioned difference, b/n mysql driver and others is stated below. Suppose this is the a simple, query:
<?php
$stmt = $db->query('SELECT * FROM table');
$row_count = $stmt->rowCount();
echo $row_count.' rows selected';
Now, let's read how the documentation states how/why that query can return count of affected_rows only when the mysql driver
NOTE: Though the documentation says this method is only for returning
affected rows from UPDATE, INSERT, DELETE queries, with the PDO_MYSQL
driver (and this driver only) you can get the row count for SELECT
queries. Keep this in mind when writing code for multiple databases.
This is because MySQL's protocol is one of the very few that give this
information to the client for SELECT statements. Most other database
vendors don't bother divulging this information to the client as it
would incur more overhead in their implementations.
So, there is that little difference, as far as I know.