How Do You Return Basic Messages from a SQL Query to a PHP Web Page When Using LAPP?

Problem scenario
You have deployed the LAPP stack. You want a basic and meaningful message to be presented to a user of the web application after a SQL command has been run.  Currently a user can click on buttons to run SQL commands behind the scenes.  Messages that are returned to the user may be meaningless like "Resource id #4" or "Resource id #5."  You want to augment the functionality so at least a rudimentary message (with some meaning) is returned to the user.  This way he/she will know if the query was successful or not.  How do you do this?

Solution
Assuming that you are invoking SQL commands with the pg_query command (see this link to deploy the LAPP stack), assign the output to a variable like this:

$varcomplete = pg_query($query);

Then add these two lines of code to your PHP page that is displayed after the user clicks the button to invoke a SQL command:

if ($varcomplete != '') {$varcomplete = "The query was successful";}
else {$varcomplete = "WARNING.  The query failed!!!";}

Leave a comment

Your email address will not be published.