Can I write a custom function that I can call when making mySQL queries eg.
My database table has fields ‘filetype’ and ‘filename’. I want to perform a function on filename before returning the result.
$query = 'SELECT filetype, MY_FUNCTION(filename) FROM table..'; $result = mysql_query($query); return $result
SO $result now has the new values created by MY_FUNCTION. The original database data will be unchanged.
OK – so it looks like User Defined Functions are the way to go… how do I write my own user defined function? Can I use PHP to write it? Where do I save the function or do I include it in my globals file?
Thanks!
To create the function, you just paste its code (sililar to provided above) in your favorite query tool, like
mysql.exe,MySQL Query Browser,phpmysqladminetc.You can write
UDF‘s both inCand inSQL. In the latter case, you don’t need to have a compiler.The function I provided as an example is an
SQL UDF. It just returns first20characters of the filename.You can use any
MySQLfunctions in anSQL UDF.