SQL Syntax error, when executing several queries? - php

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

Related

How can I fix this syntax error on my PHP MySQL query

I have been trying to make a query in MYSQL, I am doing 2 things in 1 query. My host has a Limit to queries so I need to limit the amount of queries I am doing. When I did the query 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 ', (UPDATE users SET last_online=NOW() WHERE
username='Welcome_234')' at line 1
I have put a semicolon ";" but it still doesn't work (This query worked in phpMyAdmin).
The code for the query is:
SELECT * FROM `users` WHERE username='Welcome_234',
UPDATE `users` SET last_online=NOW() WHERE username='Welcome_234'
I have tried many hours to figure out the problem and I still haven't found it.
Most PHP interfaces to run MySQL queries don't support multiple queries per call. You must run them one query per call.
Your host's limit on queries can't be so low that the difference between one and two queries will break it. I would guess the limit is in place to prevent clients from running excessive loops that run thousands of queries. If it is so low that you can't run two queries, then get a different host.

Dynamic SQL query - > row data to column name

I have a little/big problem with dynamic SQL query/ row datas to column name.
I have this link:
http://sqlfiddle.com/#!9/b4478/1
But if i adding VERY more datas, the query not working.
I getting 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 'FROM test
GROUP BY nid' at line 2
my database is big currently.
in the next days i making the empty database.
i adding to names(another table, connected id to "nid"), date(datum), amount(osszeg).
week to week the data is too much(maybe but once too much, and then not working the query)
and i need a simple/single PHP query code for this query.
Thanks for helping and sorry my bad english.
Very thanks!
Without seeing the actual SQL statement, we're just guessing. But I suspect that you are running into documented behavior of the GROUP_CONCAT function, which returns a string of maximum length specified by the MySQL variable GROUP_CONCAT_MAX_LEN. The default is 1024 bytes.
Reference: https://dev.mysql.com/doc/refman/5.6/en/server-system-variables.html#sysvar_group_concat_max_len
SET group_concat_max_len = 1024;
For debugging, you could have the MySQL stored program to emit the contents of #sql, instead of attempting to prepared it, e.g.
SELECT #sql;
You may want to consider comparing the length (in bytes) of the string returned from the GROUP_CONCAT function is not equal to the maximum length GROUP_CONCAT_MAX_LEN, to check whether the value has been truncated.
On a different note, I would opt for returning this result as a set of rows, rather than as columns on a single row. And I would do any required translation for display on the client side, rather than doing it in the database.

Stumped on why MySQL won't accept my query?

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'

Use php uniqid in mysql table name results in occasional errors - how do I avoid it?

Without getting into the lengthy details of why, I have a need to auto-generate mySQL tables that utilize a unique table name that incorporates the string generated by PHP uniqid function. When doing so, I occasionally (not always) get the following query error:
Invalid query: 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 ''512e1d9518d44_tbl'' at line 1 Whole query: SELECT
SUM(p_count) AS 'pcnt' FROM 512e1d9518d44_tbl
I know I could use a simple cross reference lookup table, but is there another way to avoid the error, which I believe is the result of a violation of table naming rules, while still maintaining the table naming non-squential uniqueness? I've tried single quoting the table name but get the same result btw.
When your table / column name starts with a number, you have to escape it using backticks:
SELECT SUM(p_count) AS pcnt FROM `512e1d9518d44_tbl`

PHP/MySQL returns no result and no error

I have a single MySQL query created from PHP 5. The Query has 3 SELECT and 2 JOIN clauses. It accesses two databases on a single host using one connection and db1.table1 db2.table2 techniques. I echo the query before running. In PHP the query returns no result and does not error. When I copy and paste the echoed query into SQL in PHPMyAdmin, it returns the correct result.
Why is PHP different to the SQL part of PHPMyAdmin and does anyone have any suggestions about getting it to work in PHP?
Why is PHP different to the SQL part of PHPMyAdmin
It's not.
You're connecting to the wrong database, writing the wrong query, or checking for errors incorrectly.

Categories