I have a MYSQL query running that returns a single string of data but it is returned as an array.

What I need to do is store that single piece of data in a variable. I know it's probably a really simple process but it has been irritating me for far too long.

the result I get when I use print_r($result) is:

Array ( [ADDDATE('2007-04-05', INTERVAL 6 DAY)] => 2007-04-11 )

I can't figure out how to store the 2007-04-11(the result) into a single variable named $end_date.

Please Help!
I know this is a quick one for most of you guys and it's gonna kill me when I see the answer but please save my sanity!

thanx in advance!

    list($myVariable) = mysql_fetch_row($queryResult);
    

      Not sure why but that didn't work.

      When I print/echo the $end_date variable at the end nothing gets printed where it should appear.

      Here is the code I have for the calculation:

      <?php
      
      // date form input
      $start_date = $_POST['DateForm'];
      
      $date_result = mysql_query("SELECT ADDDATE('$start_date', INTERVAL 6 DAY)");
      
      list($end_date) = mysql_fetch_row($date_result);
      
      print "<br>Start Date: ".$start_date.", End Date:  ".$end_date."<br>";
      
      ?>
      

        You might want to do some defensive coding to make sure things are really working as expected:

        $start_date = $_POST['DateForm'];
        $sql = "SELECT ADDDATE('$start_date', INTERVAL 6 DAY)";
        $date_result = mysql_query($sql) or die("Query failed ($sql): " . mysql_error();
        if(mysql_num_rows($date_result))
        {
           list($end_date) = mysql_fetch_row($date_result);
           print "<br>Start Date: ".$start_date.", End Date:  ".$end_date."<br>";
        }
        else
        {
           user_error("No results returned by query ($sql)", E_USER_WARNING);
        }
        
          NogDog wrote:

          You might want to do some defensive coding to make sure things are really working as expected:

          $start_date = $_POST['DateForm'];
          $sql = "SELECT ADDDATE('$start_date', INTERVAL 6 DAY)";
          $date_result = mysql_query($sql) or die("Query failed ($sql): " . mysql_error();
          if(mysql_num_rows($date_result))
          {
             list($end_date) = mysql_fetch_row($date_result);
             print "<br>Start Date: ".$start_date.", End Date:  ".$end_date."<br>";
          }
          else
          {
             user_error("No results returned by query ($sql)", E_USER_WARNING);
          }
          

          When I tried this there was still no output from the $end_date variable or from the user_error.

          I then tried to implode the $date_result and save that as $end_date but still no joy.

          $end_date = implode("",$date_result);

          Getting really confused with this one as it seemed like it would be a simple enough task.

            Try

            $result = mysql_result(mysql_query('your query'), 0);
            

            This will immediately place the result in the variable...

              TitanKing wrote:

              Try

              $result = mysql_result(mysql_query('your query'), 0);
              

              This will immediately place the result in the variable...

              Thank you so much. Such a simple solution and now I'm kicking myself that I didn't think of it.

              I can get back to being sane now.

              Thanx again!

                Write a Reply...