Score:-1

How can select data from mysql using bash script and pass it to php?

in flag

This is what Im trying now

In PHP Im running the following sript:

exec('/home/user1/createinstanceinfolder.sh', $output, $return_var);

print_r($output);

Which is calling the following Bash script:

declare -A arr == $(sudo mysql -u root -h localhost -e "USE mydb;SELECT * FROM users")

for i in "${arr[@]}"
do
echo "$i"
done

Php is printing a empty array, what is wrong here?

hr flag
Your script appears to have no shebang - are you sure php even interprets it with bash? What exactly do you expect `declare -A arr == $( ...)` to do? is the output of your mysql command suitable for assignment to an *associative* array? In any case, array assignments should look like `arr=( ... )`. Why are you putting stuff into an array, only to echo it back out? Did you test the script **outside** of php?
cn flag
Why would you do this using BASH and not PYTHON? BY FAR easier
Iosef avatar
in flag
Here is how my bash script looks like now #!/bin/bash declare -a arr = $(sudo mysql -u root -h localhost -e "USE mydb;SELECT * FROM users") for i in "${arr[@]}" do echo "$i" done Im new in ubuntu, Im not sure how to test it outside of PHP. What I want to do is to retrieve the info from mysql and return it to PHP
Iosef avatar
in flag
After retrieving this the bash scripts will also be responsible to create folders in var/www/mysite based on what fund in the database. I want to keep mysite with 755 and also dont want to give write permission to the web server thats why Im doing all this in BASH
Iosef avatar
in flag
This is what Im getting outside of php: ./createinstanceinfolder.sh: line 7: declare: `=': not a valid identifier ./createinstanceinfolder.sh: line 7: declare: `5': not a valid identifier ./createinstanceinfolder.sh: line 7: declare: `111111111111': not a valid identifier ./createinstanceinfolder.sh: line 7: declare: `[email protected]': not a valid identifier
Score:1
in flag

I fund the right syntax is:

#!/bin/bash

set -f        # disable globbing
IFS=$'\n'     # set field separator to NL (only)
arr=($(sudo mysql -u root -h localhost -e "USE mydb;SELECT * FROM users"))
 
for i in "${arr[@]}"
do
   echo "$i"
done
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.