Search code examples
mysqlphing

Is it possible to perform a MySQL query using Phing and set the value as a property?


I'm new to Phing.

I'd like to query a value in a MySQL database table, and have the value set as a property so that I can echo it out nicely to the screen.

I can see that there is a PDOSQLExecTask which would allow me to run some SQL, but I can't see how to set the returned value into a property?

The query I want to run is: SELECT MAX(change_number) FROM changelog;

I'd like it set into a property:

Can anyone shed any light please?

Thanks, Chris


Solution

  • I have access to MySQL at command line, I went with the following solution. I'm sure it's not the best, if someone else can improve it please do!

                <!-- What's the latest delta that's been applied to this deployment? -->
        <exec
            command="${progs.mysql} -h${db.host} -u${db.user} -p${db.pass} -e 'USE ${db.main_db}; SELECT MAX(`change_number`) FROM `changelog`;'"
            dir="."
            checkreturn="false"
            passthru="false"
            outputProperty="latest_version_output"
        />
        <php expression="preg_replace('/[^0-9]|\r|\n/si', '', '${latest_version_output}');" returnProperty="latest_version_applied" />
        <echo msg="Latest delta applied was: ${latest_version_applied}" />