I have this PHP:
exec("/csvexport.sh $table");
Which runs this bash script (it exports a table to CSV):
#!/bin/bash
table=$1
mysql --database=db --user=user --password=pass -B -e "SELECT field1, field2, IF(field3 = '0000-00-00','0001-01-01',field3) AS field3 FROM mytable;" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > /home/backups/$table.csv
This works fine. However I want the query to be dynamic like so:
$query = "SELECT field1, field2, IF(field3 = '0000-00-00','0001-01-01',field3) AS field3 FROM mytable;";
exec("/csvexport.sh $query $table");
If I change my bash like so:
#!/bin/bash
query=$1
table=$2
mysql --database=db --user=user --password=pass -B -e "$query" | sed "s/'/\'/;s/\t/\",\"/g;s/^/\"/;s/$/\"/;s/\n//g" > /home/backups/$table.csv
Even though everything is "identical" it gives the following error:
sh: -c: line 0: syntax error near unexpected token `('
So I'm thinking it isn't happy with how the query contains brackets when passed from PHP?
mysqli
and can handle cli parameters.