I’ve got a shop where users can purchase virtual items, such as one time power-up’s and permanent changes to the users account. These purchases potentially alter different fields in different tables, and there’s the possibility for a lot of items so i don’t want to code a new addition to a PHP file every time i want to add a new product.
I’m thinking of saving the query as a string into a MySQL database, then when an item is purchased i can fetch the string from the database and run it like that.
For example: A table items would contain a field query_to_run which would contain the string UPDATE power_ups SET test = test WHERE test = 1 to be run when a user purchases an item.
Is this ever used or is there a better way to do this?
If I were to tackle this problem myself I would use OOP PHP to define a power-up class with different types of power-up categorised by the type of database edits they must make.
What I think you are looking for is something that is quicker, and your solution is this (in the short-term) however what happens when you update the database structure or realise your old model doesn’t fit when you wish to make improvements?
Because your SQL queries are hard-coded you will have to rewrite each and every one, and each new power up takes longer than if you built a soft-coded framework around them.
So yes your method would work in practice as long as you don’t mind sacrificing the extensibility and flexibility of your application in the future.