View Full Version : Please clairfy use of MySQL functions in PHP

07-15-2008, 10:09 PM
Im getting a bit mixed up with PHP and MySQL. I understand PHP is the scripting language and MySQL is the database server software.

I know mysql functions are used within PHP to interact with the database - but can I use any MySQL function? For example I wanted to use the DAYNAME() function to get the day name of a date for a variable in my PHP script. But using this function doesnt work.

Am I missing something? How can I use such functions with PHP - or cant I?

07-15-2008, 10:22 PM
OK - I think Ive answered my own question - MySQL functions do not work in PHP - but PHP has many of its own functions to do the same things and many of them are designed to interact with MySQL databases.

Am I right?

07-15-2008, 10:23 PM
Yes, Mysql functions are dictated as a part of their string query. So any function calls (as long as you put them within a string) will be understood as only a part of the query and mysql will handle the rest. PHP ignores anything defined as a string and does not attempt to parse it:

$sQry = 'SELECT SUM(price) FROM priceTable';

PHP ignores this SUM as its a string call, but MySQL will interpret it as an aggregate function call.

You cannot however use SQL functions as a part of PHP (which is what it sounds like you are doing). Calling DAYNAME is considered undefined in the scope of PHP:


Will result in a fatal error since DAYNAME has not been defined. To parse PHP dates, use the date (http://ca3.php.net/manual/en/function.date.php) function:


echo date('l');
// Returns Tuesday, since today is Tuesday July 15, 2008


You could execute a query to give you this result, but thats way too much overhead for something that can be handled by PHP alone.