Help with a complex mysql command executed on php

Discussion in 'PHP' started by more_sem, Oct 7, 2010.

  1. #1
    Hi, i am trying to connect two tables and run a function along side
    the two tables are users and buddies
    i am using a custom php getage function to determine age which is used in the mysql command too
    the getage function is
    
    function getage($strdate)
    {
        $dob = explode("-",$strdate);
        if(count($dob)!=3)
        {
          return 0;
        }
        $y = $dob[0];
        $m = $dob[1];
        $d = $dob[2];
        if(strlen($y)!=4)
        {
          return 0;
        }
        if(strlen($m)!=2)
        {
          return 0;
        }
        if(strlen($d)!=2)
        {
          return 0;
        }
      $y += 0;
      $m += 0;
      $d += 0;
      if($y==0) return 0;
      $rage = date("Y") - $y;
      if(date("m")<$m)
      {
        $rage-=1;
        
      }else{
        if((date("m")==$m)&&(date("d")<$d))
        {
          $rage-=1;
        }
      }
      return $rage;
    }
    
    Code (markup):
    the two tables to be used with the necessary fields are users(id,name,birthday) buddies(uid,tid). The sql which i have come up with is
    
    $sql = "SELECT
              a.id,a.name,a.birthday, b.uid, b.tid FROM users a
              INNER JOIN buddies b ON a.id = $who AND a.id = b.uid OR a.id = b.tid
              AND month(`birthday`) = month(curdate()) AND dayofmonth(`birthday`) = dayofmonth(curdate())
              GROUP BY 1,2 ORDER BY a.name ASC 
              LIMIT 0, 10
        ";
    
    Code (markup):
    $who variable is the id of the person and i wish to check if any of his buddies has a birthday on the current day. for which i need to tally the id of users with uid or tid of buddies. Can anybody please direct me in correcting this code ?? The function isnt a problem, the sql is a problem . Please help...
     
    more_sem, Oct 7, 2010 IP