Publishing System Settings Logout Login Register
Function Based MySQL Statements
TutorialCommentsThe AuthorReport Tutorial
Tutorial Avatar
Rating
Add to Favorites
Posted on September 6th, 2010
2266 views
PHP Coding

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):

function alter_table($table, $query, $where){

mysql_query("UPDATE $table SET $query WHERE $where");

}

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

Dig this tutorial?
Thank the author by sending him a few P2L credits!

Send
Aalicki

This author is too busy writing tutorials instead of writing a personal profile!
View Full Profile Add as Friend Send PM
Pixel2Life Home Advanced Search Search Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Tutorial Index Publish Tutorials Community Forums Web Hosting P2L On Facebook P2L On Twitter P2L Feeds Pixel2life Homepage Submit a Tutorial Publish a Tutorial Join our Forums P2L Marketplace Advertise on P2L P2L Website Hosting Help and FAQ Topsites Link Exchange P2L RSS Feeds P2L Sitemap Contact Us Privacy Statement Legal P2L Facebook Fanpage Follow us on Twitter P2L Studios Portal P2L Website Hosting Back to Top