Php script for retrieving inserting updating and deleting mysql table
Obviously, this is only a primitive example, but it shows how to use the active record pattern to perform conditional SELECT statements with Code Igniter.In a similar fashion, the following sample application fetches all the users whose IDs are greater than 2. (definition for ‘users.php’ file – located at /system/application/models/ folder) class Users extends Model (definition for ‘users.php’ file – located at /system/application/controllers/ folder) class Users extends Controller (definition for ‘users_view.php’ file – located at /system/application/views/ folder) Having listed the full source code for the two previous web applications, I should assume that running conditional SQL queries using the active record pattern is now a familiar topic to you, right?All of them worked with a sample “users” My SQL table, whose structure was similar to the following: Okay, now that you remember how the table looked, here’s the complete source code corresponding to the first example.It fetches all of the users whose IDs are less than 5.To dissipate any possible doubts about how to use these method, I coded two controllers.The first one updates an existing row of a sample My SQL table, and the second one deletes a specified record.To learn the complete details of how to perform these database operations with Code Igniter, please read the upcoming section. True to form, updating and deleting database rows with the active record pattern is only a matter of using a couple of intuitive methods, called “update()” and “delete()” respectively.They can be included in a simple controller class, as I did before when explaining how to perform database insertions.
Each of these examples was comprised naturally of three source files: a model, a controller class, and finally a view file.
However, as I said before, Code Igniter lets you perform inserts, updates, and deletion operations using this design pattern as well.
Therefore, in the next few lines I’ll be discussing these interesting topics, as always, by means of several code samples. Actually, before I proceed to demonstrate how to perform inserts, updates and deletions with the active record pattern, I’d like to reintroduce a pair of practical examples developed in the previous tutorial.
Eventually, I could define a couple of additional views for displaying either an error message or a confirmation web page, depending on the result of the insert operation.
However, this will be left momentarily as homework for you, in case you feel bored and want to have some fun coding PHP classes.
Indeed, this class not only allows you to run queries using a traditional approach; it also allows you to select, insert, update and delete database records by means of the active record pattern, which lets you avoid coding SQL clauses explicitly.