Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. MySQL
  4. DBI call to mysql, trying to get return value from select...

DBI call to mysql, trying to get return value from select...

Scheduled Pinned Locked Moved MySQL
perldatabasemysqltutorialquestion
8 Posts 2 Posters 28 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    roy holliday
    wrote on last edited by
    #1

    trying to get a return value in Perl using DBI to use Mysql's date functions... can't find the result... code : $statement = $db_handle->prepare("SELECT ADDDATE($string, 7); "); I can't figure out how to get the value to see/use/print... tried a few things... anyone got a sample of doing this? thanks...

    G 1 Reply Last reply
    0
    • R roy holliday

      trying to get a return value in Perl using DBI to use Mysql's date functions... can't find the result... code : $statement = $db_handle->prepare("SELECT ADDDATE($string, 7); "); I can't figure out how to get the value to see/use/print... tried a few things... anyone got a sample of doing this? thanks...

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      This is more of a Perl question than MySQL, but here goes... If you use prepare, you have to use execute too, and then a fetch function to get data back:

      $statement = $db_handle->prepare("SELECT ADDDATE($string,7)");
      $statement->execute;
      @data = $statement->fetchrow_array;
      $new_date = $data[0];

      There are shorter functions for doing simple selects:

      @data = $db_handle->selectrow_array("SELECT ADDDATE($string,7)");
      $new_date = $data[0];

      All of this is covered quite well in the Perl DBI manual page[^].

      R 1 Reply Last reply
      0
      • G Graham Breach

        This is more of a Perl question than MySQL, but here goes... If you use prepare, you have to use execute too, and then a fetch function to get data back:

        $statement = $db_handle->prepare("SELECT ADDDATE($string,7)");
        $statement->execute;
        @data = $statement->fetchrow_array;
        $new_date = $data[0];

        There are shorter functions for doing simple selects:

        @data = $db_handle->selectrow_array("SELECT ADDDATE($string,7)");
        $new_date = $data[0];

        All of this is covered quite well in the Perl DBI manual page[^].

        R Offline
        R Offline
        roy holliday
        wrote on last edited by
        #3

        thanks, I've been using DBI now for several years... My question was more for the syntax to retrieve function values from MySql... In this case, the SQL manual gave it "command line", and I didn't see how to retrieve it via DBI.. I can do this with Oracle "select "oracle-sequence-name" from dual", for example... I tried both of your examples and both returned nothing... I am researching further, but wanted to ask if you had run these? if so, I can take to my hosting support and see if they can assist in finding out why they return no value... thanks so much... Roy

        G 1 Reply Last reply
        0
        • R roy holliday

          thanks, I've been using DBI now for several years... My question was more for the syntax to retrieve function values from MySql... In this case, the SQL manual gave it "command line", and I didn't see how to retrieve it via DBI.. I can do this with Oracle "select "oracle-sequence-name" from dual", for example... I tried both of your examples and both returned nothing... I am researching further, but wanted to ask if you had run these? if so, I can take to my hosting support and see if they can assist in finding out why they return no value... thanks so much... Roy

          G Offline
          G Offline
          Graham Breach
          wrote on last edited by
          #4

          Ah, OK. In that case the problem is probably what $string contains. SELECT ADDDATE(2010-01-21, 7) - returns NULL, because 2010-01-21 is a numeric expression, and MySQL expects a date. SELECT ADDDATE('2010-01-21', 7) - returns 2010-01-28, as expected. So you can wrap the date string in quotes, or use placeholders:

          $statement = $db_handle->prepare("SELECT ADDDATE(?,7)");
          $statement->execute($string);

          I've tried both methods out, and they are working for me with $string = '2010-01-21'.

          R 3 Replies Last reply
          0
          • G Graham Breach

            Ah, OK. In that case the problem is probably what $string contains. SELECT ADDDATE(2010-01-21, 7) - returns NULL, because 2010-01-21 is a numeric expression, and MySQL expects a date. SELECT ADDDATE('2010-01-21', 7) - returns 2010-01-28, as expected. So you can wrap the date string in quotes, or use placeholders:

            $statement = $db_handle->prepare("SELECT ADDDATE(?,7)");
            $statement->execute($string);

            I've tried both methods out, and they are working for me with $string = '2010-01-21'.

            R Offline
            R Offline
            roy holliday
            wrote on last edited by
            #5

            get out your nominations for idiot of the year (ME!)... I spelled the result name wrong in my print statement... everything works fine... thanks so much for the resolution, I have been pounding away on this for a while...

            1 Reply Last reply
            0
            • G Graham Breach

              Ah, OK. In that case the problem is probably what $string contains. SELECT ADDDATE(2010-01-21, 7) - returns NULL, because 2010-01-21 is a numeric expression, and MySQL expects a date. SELECT ADDDATE('2010-01-21', 7) - returns 2010-01-28, as expected. So you can wrap the date string in quotes, or use placeholders:

              $statement = $db_handle->prepare("SELECT ADDDATE(?,7)");
              $statement->execute($string);

              I've tried both methods out, and they are working for me with $string = '2010-01-21'.

              R Offline
              R Offline
              roy holliday
              wrote on last edited by
              #6

              OK, I can get it to work with the date coded into the select, but not with a var... Here's the code: $string='\'2010-04-16\''; print 'string val=',$string,''; $statement = $db_handle->prepare("SELECT ADDDATE(?,7)"); $statement->execute($string); @data = $statement->fetchrow_array; $new_date = $data[0]; print '<br>new date=',$new_date,''; I run this and get nothing... does this work as is for you?

              G 1 Reply Last reply
              0
              • G Graham Breach

                Ah, OK. In that case the problem is probably what $string contains. SELECT ADDDATE(2010-01-21, 7) - returns NULL, because 2010-01-21 is a numeric expression, and MySQL expects a date. SELECT ADDDATE('2010-01-21', 7) - returns 2010-01-28, as expected. So you can wrap the date string in quotes, or use placeholders:

                $statement = $db_handle->prepare("SELECT ADDDATE(?,7)");
                $statement->execute($string);

                I've tried both methods out, and they are working for me with $string = '2010-01-21'.

                R Offline
                R Offline
                roy holliday
                wrote on last edited by
                #7

                I got it to work with the var in the select statement... this is fine, I'm able to work with this.... thanks for your assistance

                1 Reply Last reply
                0
                • R roy holliday

                  OK, I can get it to work with the date coded into the select, but not with a var... Here's the code: $string='\'2010-04-16\''; print 'string val=',$string,''; $statement = $db_handle->prepare("SELECT ADDDATE(?,7)"); $statement->execute($string); @data = $statement->fetchrow_array; $new_date = $data[0]; print '<br>new date=',$new_date,''; I run this and get nothing... does this work as is for you?

                  G Offline
                  G Offline
                  Graham Breach
                  wrote on last edited by
                  #8

                  The problem here is that the date string already contains the quotes - using the placeholder binding means the database driver does the quoting for you. To make your code above work, you just need to replace the $string='\'2010-04-16\''; with $string='2010-04-16';.

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups