Can I run PHP on MySQL paste?

I really want to send mail or somehow notify the user when an entry has been added to the database. Is it possible? As an ON INSERT trigger, am I running some PHP?

+4
source share
5 answers

You cannot execute a PHP script with mysql events.

You should do this at the end of PHP.

Check if the insert command completed successfully and send mail.

+3
source

Just check if the insert is successful, and then send mail to the client.

+2
source

you cannot run php in the mysql query, but you can check if the query was successful and then send someone, mysql_query() will return boolean ( true or false ). if you use php, use it in the correct way;)

 if(mysql_query('// your query here')){ //mail someone } 
+1
source

You can execute external programs from MySQL triggers by setting MySQL sys_exec UDF

However, this seems like overhead. Why should you force MySQL to trigger a notification when it creates PHP, and at this point you know that you entered the data?

+1
source

The fastest and easiest way to generate notifications (emails, etc.) about inserts / changes / deletions of a database is to make it from a script that performs a change in the database.

 <?php $res = mysql_query( 'INSERT INTO `table` ( `field1` ) VALUES ( "One" )' ); if( $res ){ # Send Notification } ?> 

Otherwise, if you are not doing database manipulations yourself (for some reason) or want to perform computed summaries (hourly, daily, weekly), you will need to use something like Cron Job to query the database and check for changes .

 <?php # A file containing an integer which is the Highest ID in the table $indexFile = 'lastIndexID.txt'; # Get that Highest ID from the file, if it exists $lastIndex = 0; if( file_exists( $indexFile ) $lastIndex = (int) file_get_contents( $indexFile ); # Check the Database $res = mysql_query( 'SELECT `id` FROM `table` ORDER BY `id` DESC LIMIT 1' ); if( $res && mysql_num_rows( $res )==1 ){ $row = mysql_fetch_assoc( $res ); # Check if the ID has increased (ie new rows added) if( $row['id']>$lastIndex ){ # Send Notification # The number of New Rows will be the difference between $row['id'] and $lastIndex # Update the Index File file_put_contents( $indexFile , $row['id'] ); } }else{ # An Error Occurred } ?> 
0
source

All Articles