I am new to writing my own functions, but am quite familiar with both PHP and MySQL otherwise.
I am updating a site that has tons of instances of the following basic code:
$sql = "select DATE_FORMAT(datepublished, '%W, %M %y') from news where id = $id";
$result = mysql_query($sql);
$nicedate = mysql_result($result, 0);
I'd like to replace all of this junk with a custom function (as it appears several times on each page). I wrote this and put it in my constants.php file:
function formatdatetime ($tablename, $fieldname, $format, $selector)
{
$sql = "select DATEFORMAT($fieldname, '$format') from $tablename where id = $selector";
$result = mysql_query($sql);
echo mysql_result($result, 0);
}
It doesn't work, I think because the server is trying to evaluate the function as a literal query when it includes the constants.php file.
Can anyone tell me what I'm doing wrong and how to accomplish this?