Posted on September 6th, 2010
2266 views We all have to write long drawn out MySQL queries at some point and time, fact is, a script can quickly become over-crowded with mysql_query(""); statements. Instead, we can write a function that will in turn, shorten out the length of a query, and is much faster type out. We will need to create a function function alter_table($table, $query, $where){} This will contain some dynamic variables in which you'll type out in the actual script. The Next part is to write out the simple structure of your basic query: mysql_query("UPDATE $table SET $query WHERE $where"); This will 'update' your records, pending one your parameters set inside of the function you call. Seems simple so far, and it is! Let's put together the entire function (yes, it's still really simple):
Now that we have wrote the function, we'll need to make a call to it in a script, this next part requires a little effort. alter_table("table_name", "row = 'test'", "id = '1'"); Of course, this is a very basic example. As you can see, you'll have to give a table name (table_name), and then what the query needs to do (row = 'test'), and of course, which record to update. In this case, it's a column titled 'id', and any instance where 'id' equals (=) 1 |