Find the answer to your Linux question:
Results 1 to 4 of 4
Hey there, i have a tricky one here i am having trouble passing a variable to be either a field name or a value. when i run this Code: $Day ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Aug 2005
    Posts
    15

    trouble passing variable to SQL


    Hey there,
    i have a tricky one here
    i am having trouble passing a variable to be either a field name or a value.

    when i run this
    Code:
    $Day = $_GET["Day"];
    $Month = $_GET["Month"];
    $date = "2005-".$Month."-";
    $date .= $Day;
    
    function Hist($Unit, $date){    
         $result = mysql_query("SELECT Daily FROM RainGaugeHistory WHERE Sensor_ID = {$Unit} AND `Date`> {$date}");
          $num=mysql_numrows($result);
          if ($num == "FALSE"){
             $Unit = 'No History';
             return $Unit;            
          }      
          else{   
             $i=0;
             while &#40;$i < $num&#41;&#123;
                echo $i;
                echo nl2br&#40;"\n"&#41;;            
                $DayTotal = mysql_result&#40;$result,$i,"Daily"&#41;;
                $Daily = $Daily + $DayTotal;
                $i++;            
                $Today = OneDay&#40;$Unit&#41;;
                $Unit = $Daily + $Today;
                return $Unit;
             &#125;         
          &#125;
    &#125;
    
    $Lab = Hist&#40;'515',$date&#41;;
    i am trying to add up the daily totals from a daily updated history file
    trouble is, when i run it, it selects every record for the unit except for only those after the $date.
    i have tried changing the field type from date to VARCHAR(10) but that wasn't it helping either. maybe (probably) there is a much better way to do this, i am really open for any suggestion that may prevent more hair loss.

  2. #2
    Linux Guru sdousley's Avatar
    Join Date
    Feb 2004
    Posts
    1,790
    Erm, there is a better way to do it. I worked out that storing it as a 10 digit integer (if time of day doesn't matter).

    Then you can use the date() function and time() functions.

    take a look at this code (and what it does) It should help you out.

    Code:
    <?php 
    $time = time&#40;&#41;; 
    echo $time; 
    echo "
    
    "; 
    echo date&#40;'jS F Y H&#58;i&#58;s', $time&#41;;  
    echo "
    
    "; 
    echo strtotime&#40;"27 July 2005"&#41;, "\n"; 
    echo "
    
    "; 
    echo strtotime&#40;date&#40;'j F Y', mktime&#40;0, 0, 0, 12, 25, 1984&#41;&#41;&#41;;
    ?>
    "I am not an alcoholic, alcoholics go to meetings"
    Registered Linux user = #372327

  3. #3
    Just Joined!
    Join Date
    Aug 2005
    Posts
    15
    thanks, i finally got it working in testing but i didnt have to keep the date as a VARCHAR
    thats really cooll
    thanks for the clip

    <><

  4. #4
    scm
    scm is offline
    Linux Engineer
    Join Date
    Feb 2005
    Posts
    1,044
    It'd be more sensible to let MySQL SUM the field, surely?

Posting Permissions

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