I am using wpdb->insert to insert a row to the table,
like this
$wpdb->insert($table_name, array("title"=>"foo"));
$wpdb->last_query returns the below query
INSERT INTO `some_table` (`title`) VALUES ('foo')
But i get a error in sql syntax
WordPress database 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 '1' at line 1
The error is little confusing, i passed a string 'foo' in to insert statement, but somehow it is showing '1' in the error.
This error occurs when i am running the $wpdb->insert inside phpunit, i copied the query and ran it on my test wordpress database and this query works correctly in the different method in the same test suite, it works fine.Any idea how to fix this error?
Please try this code. The recommended way (as noted in codex):
$wpdb->insert($table_name,array('title'=>'foo'),array('%s'));
Related
This question already has answers here:
PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)
(7 answers)
Closed 8 months ago.
I'm trying to delete any existing rows with a specified column value if they exist and Insert a new row directly after in a single prepared statement. I have tried both binding param values and passing an array to execute();
$sql = "DELETE FROM table WHERE id = :id;
INSERT INTO table (col1,col2,col3,col4,col5) VALUES (:col1,:col2,:col3,:col4,:col5);";
I have also tried:
$sql = "DELETE FROM table WHERE id = ?;
INSERT INTO table (col1,col2,col3,col4,col5) VALUES (?,?,?,?,?);";
They work fine separately, but wont work in one statement. The exact error being thrown is:
PHP Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You
have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'INSERT INTO ...
The syntax error is pointing to the start of the second statement, so is it possible that I'm not supposed to be running a delete and insert statement after each other? I only have one version of mysql/mariadb installed and it's always been up to date, and the syntax works in separate statements. Operating system is Windows.
#Don'tPanic commented a link with a good answer:
PDO support for multiple queries (PDO_MYSQL, PDO_MYSQLND)
In my Database config model I had set the ATTR_EMULATE_PREPARES to false for some reason, and setting it to true fixed the issue and the statement executed just fine. The post in the link also advises to specify the encoding type in the dsn if you do choose to set the attribute to true.
I have renamed my database in phpMyAdmin but it makes some problems. When I tried to rename it to its first name, it asked first if I want to rename it and drop my database.
When I press OK, it give me this 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 qetary2015-02-21.gps_poss
The SQL query:
ALTER TABLE qetary2015-02-21.gps_poss
How can I fix it please?
At a guess:
ALTER TABLE `qetary2015-02-21`.gps_poss
Wrap the database name in backticks, else the - character might be misinterpreted as a minus sign.... though I hope there's slightly more to your SQL query than that snippet
The following code is an SQL query that I am using in PHP.
$sql = "INSERT INTO updates (update,user_id_fk)
VALUES ('$post_update','$username')";
It does not work - I get the following error message when it is run.
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 'update,user_id_fk)
This syntax loks fine to me - it is the same as I found on W3Schools.
The mysql verion I am using is 5.5.24
update is a reserved keyword backtick it
INSERT INTO updates (`update`,user_id_fk)
check the list below and in future do not use these words for your table or column names
http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html
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
I'm running this code in PHP:
mysql_query("SET #update_id:=NULL");
echo mysql_error();
And this is what I get:
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 '' at line 1
Also this same code runs perfectly in PHPMyAdmin. What am I doing wrong?
Additional information. I'm trying to write a query described here:
How to get ID of the last updated row in MySQL?
But the problem right now is that I even can't run a simple query to create variable.
P.S. Ok, now it seems that it desn't work because of some previous queries that are not related to this one. If i move this query to the top of the php file it works. Also if I try to make this:
mysql_query("SET #update_id:=NULL; SELECT #update_id;");
It fails with syntax error. But this works fine:
mysql_query("SET #update_id:=NULL;");
mysql_query("SELECT #update_id;");
Does somebody knows what am I missing here?
Why can't I run two commands in one query and why they're the separate queries are related to each other?
mysql_query("UPDATE your_table SET update_id=NULL");
Check this it may be helpful
SELECT #update_id IN("SET #update_id:=NULL");