Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jun 2008
    Posts
    68
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Please clairfy use of MySQL functions in PHP

    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?

  • #2
    New Coder
    Join Date
    Jun 2008
    Posts
    68
    Thanks
    8
    Thanked 0 Times in 0 Posts
    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?

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    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:
    PHP Code:

    $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:
    PHP Code:

    DAYNAME
    ($fromVar); 
    Will result in a fatal error since DAYNAME has not been defined. To parse PHP dates, use the date function:
    PHP Code:
    <?php

    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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •