0

Say I have a number of similar mysql databases, where one table consists of users, which I want to query, while the name of the specific table is also variable.

I would say I need something similar, but it gives me a hard time to get the quotes rigth:

user_id = $(mysql --user=$dba --password=$dbp $dbn -e "SELECT id FROM ${user_tbl} WHERE username = \"guest\"")

Addition: Ok, now knowing what the initial query should have been, comes the other question, how to update a field again using a variable for the tabelname.

mysql --user=$dba --password=$dbp $dbn -e "UPDATE ${user_tbl} SET password=${pass} WHERE username = 'guest'"
gives the following error: Unknown column 'some_encrypted_pass' in 'field list'

1
  • ... WHERE username = "guest" is not valid in any SQL. Simply making that a valid SQL would have solved your problem instantly. ... WHERE username = 'guest' Commented Oct 19, 2013 at 10:28

1 Answer 1

1
user_id=$(mysql --user=$dba --password=$dbp $dbn -e "SELECT id FROM ${user_tbl} WHERE username = 'guest'")

you should use single quote to search guest string.

btw, why are you using that method, simply run mysqli_query and get value in array [with fetch methods] ? that will be better and more secure and reliable.

Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. Been trying so many combinations of double quotes, single quotes with or without backslashes, that I nearly lost my sight.
yes, I thought you are using PHP code, my bad. but you can use php for easy coding if you know it.
Well, in the end I concluded that variables for tablenames are possible to some extend, and impossible beyond. So it may be time to change for PHP/Perl/Python or the like for this sort of system utilities. I was using bash :)

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.