Mike HeaversWeb, Writing, Creative Coding


Conditional insert or update in code igniter

Hi - just struggled through the following scenario and thought it might be useful info. If you every have a situation where you are looking to either insert a record into a database if the results of your query return no results, or update the record in the database if the results are true, you can use the following:

The key is the second “where” clause, inside the “if($query->num_rows() > 0).  The “where” from your $query no longer applies to your update.

Easily output database data to JSON with PHP for use in AJAX / jQuery

Building off the previous PHP tutorial in which we insert data into a mySQL database with PHP, this is a quick example of how to output data from the database to JSON, so that we can utilize it via AJAX in javascript.

Starting with the PHP script:

Here, we’re simply establishing a database connection, and then select our desired data from the table and assign it to a variable:

Next we establish a variable for the json data:

Then we check to make sure data has been returned from the

...read more

Easily insert data into DB with PHP/mySQL/jQuery

I am not a backend developer, so I’m constantly struggling with remembering the best way to insert simple data into a database. I need to do this often for prototypes running on my local host, and while I sometimes lean on frameworks such as codeigniter or expression engine to facilitate this, often these frameworks are overblown or time consuming to install, and I just want a simple method of inserting data. So, to remind myself as much as to help others, here’s a basic walkthrough. Note that

...read more

More Tutorials >