1

I would like to make a bash script which will add user to my custom table in MySQL database. I have username as script argument stored in variable $USERNAME and I need to execute this query into MySQL server:

INSERT INTO `user` VALUES ('$USERNAME', password)

I know how to execute a sql file but that won't help me when I have username in variable. The only way I can think of is to execute php file with GET values and execute sql command in php. And I'm not eve sure that with php will work as I think.

Is there any better way?

Thanks

EDIT: After your helpfull answers I went with this command which works:

mysql -ppassword --default-character-set=utf8 -e "INSERT INTO table VALUES (\"$USERNAME@\",\"password\")" database
2
  • 2
    Are you familiar with mysql -e "command"? In your case, mysql -e "INSERT INTO user VALUES ('$USERNAME', password) Commented Mar 17, 2014 at 16:14
  • I wasn't familiar with mysql command but now I am. Thanks for tip Commented Mar 18, 2014 at 12:26

2 Answers 2

1

You could try using a heredoc:

#!/bin/bash

USERNAME="example"

mysql <<MYSQL
INSERT INTO user VALUES ('$USERNAME', password);
MYSQL
Sign up to request clarification or add additional context in comments.

1 Comment

I didn't know about heredoc before. I looked it up and it seems good but I'll use it on some more difficult queries.
0

The command would be:

mysql -pyourpasswordwithoutspaces -e "Your insert query goes here with semicolon;"

I'm not sure if the semicolons are necessary or not, but it always good to provide them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.