Home > Mobile >  PHP exec | mysql command returns sh: -c: line 0: syntax error near unexpected token `newline'
PHP exec | mysql command returns sh: -c: line 0: syntax error near unexpected token `newline'

Time:05-11

I want to make multiple insertions into DB, but mysql command returns me: sh: -c: line 0: syntax error near unexpected token `newline'.

Does anybody know, what this error means?

Here is my command:

mysql -h 127.0.0.1 -D db_name -u username --password=pass --debug-info true <shared/local_sql/3.sql>

Example of SQL file:

DELETE FROM table_name WHERE num = '999999';
START TRANSACTION;
INSERT INTO table_name SET key1 = 'val1', key1 = 'val1', key1 = 'val1', key1 = 'val1', key1 = 'val1', key1 = 'val1', key1 = 'val1', key1 = '', key1 = '', key1 = 'val1', num = '999999';
INSERT INTO table_name SET key1 = 'val2', key1 = 'val2', key1 = 'val2', key1 = 'val2', key1 = 'val2', key1 = 'val2', key1 = 'val2', key1 = '', key1 = '', key1 = 'val2', num = '999999';
COMMIT;
UPDATE table_name SET num = '999998' WHERE num = '2';
UPDATE table_name SET num = '2' WHERE num = '999999';
DELETE FROM table_name WHERE num = '999998' OR num = '999999';

CodePudding user response:

Use

mysql -h 127.0.0.1 -D db_name -u username --password=pass --debug-info true shared/local_sql/3.sql

You probably copied the characters < and > from some example where these characters are used to indicate a placeholder for the file name like mysql ... <sql_file>.

The whole placeholder <sql_file> must be replaced with the actual file name which is shared/local_sql/3.sql in your example.

The characters < and > are interpreted by the shell as redirection of standard input or standard output.

<shared/local_sql/3.sql instructs the shell to redirect the standard input of the mysql command from the specified file. This may actually work.

> needs a file name after it to specify where the output should be redirected to. Since there is no file name but the end of the line you get the error message.

CodePudding user response:

> is the output redirection operator, and you don't set any output file. A shorter test case:

$ >
sh: syntax error: unexpected newline

If you really want to send output to a file you need to add the file name, but I have the impression that you were thinking of <> as a kind of quotes, which they aren't. Get rid of >:

mysql -h 127.0.0.1 -D db_name -u username --password=pass --debug-info true < shared/local_sql/3.sql
  • Related