Location: PHPKode > scripts > phpMyAccess > phpmyaccess-1.5.3/phpmyaccess/pma-sqlinc.php
<?php

function set_values_from_table($database,$table,$key,$desc='',$sel=''){
  if ($desc==''){
    $desc=$key;
  }
  $qry = 'SELECT DISTINCT '.$key.','.$desc.' FROM '.$table.$sel.' ORDER BY '.$desc;
  pma_debug ("Set values from query: $qry ");
  $res = mysql_db_query($database,$qry);
  if ($res) {
    while($row = mysql_fetch_row($res)) {
        $values['option'][]=$row[0];
        $values['value'][]=$row[1];
    }
  } else {
    pma_debug ("Set val from table query Error : " . mysql_errno(). ": " . mysql_error() );
  }
  return $values;
}

function get_distinct_values_sorted_from_table($db,$tb,$column){
  $qry = 'SELECT DISTINCT '.$column.' FROM '.$tb.' ORDER BY '.$column;
  pma_debug ("get_distinct_values_sorted_from_table $qry");
  $res = mysql_db_query($db,$qry);
  if ($res) {
    while($row = mysql_fetch_row($res)) {
        $values[].=$row[0];
    }
  } else {
    pma_debug ("Set val from table query Error : " . mysql_errno(). ": " . mysql_error() );
  }
  return $values;
}

function get_linked_val($db,$ltable, $lsel,$lkey,$lval) {
  $qry = 'SELECT '.$lsel.' FROM '.$ltable .' WHERE '.$lkey . '=' . $lval;
  pma_debug ("Linked Value query : $qry ");
  $res = mysql_db_query($db,$qry);
  if ($res) {
     list($row) = mysql_fetch_row($res);
     return $row;
  } else {
     $qry = 'SELECT '.$lsel.' FROM '.$ltable .' WHERE '.$lkey . '="' . $lval . '"';
     pma_debug ("Linked Value query : $qry");
     $res = mysql_db_query($db,$qry);
     if ($res) {
        list($row) = mysql_fetch_row($res);
        return $row;
     } else {
        pma_debug ("get linked val query : " .mysql_errno().": ".mysql_error() );
        return 'ERROR';
     }
     pma_debug ("Get linked val query : " .mysql_errno().": ".mysql_error() );
     return 'ERROR';
  }
}

function get_table_val($db, $table, $column, $desc, $idx) {
  $qry = 'SELECT '.$desc.' FROM '.$table .' WHERE '.$column . '=' . $idx;
  pma_debug ("Table Value query : $qry");
  $res = mysql_db_query($db,$qry);
  if ($res) {
     list($row) = mysql_fetch_row($res);
     return $row;
  } else {
     $qry = 'SELECT '.$desc.' FROM '.$table .' WHERE '.$column . '="' . $idx .'"';
     pma_debug ("Table Value query : $qry");
     $res = mysql_db_query($db,$qry);
     if ($res) {
       list($row) = mysql_fetch_row($res);
       return $row;
     } else {
       pma_debug ("Get table val query : " .mysql_errno().": ".mysql_error() );
       return 'ERROR';
     }
  }
}

function get_distinct_values_column_list($db, $table, $column, $order) {
  $qry = 'SELECT DISTINCT '.$column.' FROM '.$table . " ORDER BY " . $order;
  pma_debug ("Distinct Column values query : $qry ");
  $res = mysql_db_query($db,$qry);
  if ($res) {
     while(list($row) = mysql_fetch_row($res)) {
         $x[]=$row;
     }
     return $x;
  } else {
     pma_debug ("Distinct Column values query : " .mysql_errno().": ".mysql_error()  );
     return 'ERROR';
  }
}

function fully_qualified_name($master_table,$field_defs,$field_name, $type){
  if ($type=='view') {
    if (isset($field_defs[$field_name]['values']['description'])) {
       $fqn=$field_defs[$field_name]['values']['table'].'.'.$field_defs[$field_name]['values']['description'];
    // Selectview should probably require fully qualified names in the sql expression
    // } elseif (isset($field_defs[$field_name]['values']['selectview'])) {
    //   $fqn=$field_defs[$field_name]['values']['table'].'.'.$field_defs[$field_name]['values']['selectview'];
    }else {
       $fqn.=$master_table.'.'.$field_name;
    }
  } else {
     $fqn.=$master_table.'.'.$field_name;
  }
  return $fqn;
}

function create_column_list($master_table,$field_defs) {
  while (list($a,$b) = each($field_defs)) {
    $GLOBALS['fds'][] = $a;
  }
  $num_fds = sizeof($GLOBALS['fds']);
  $select='SELECT DISTINCT';
  $kwd = ' ';
  for ($k=0 ; $k<$num_fds; $k++)
  {
    $fd = $GLOBALS['fds'][$k];
    $select=$select.$kwd.fully_qualified_name($master_table,$field_defs,$fd,"view");
    $kwd = ',';
  }
  return $select;
}

function create_column_list_change($master_table,$field_defs) {
  while (list($a,$b) = each($field_defs)) {
    $GLOBALS['fds'][] = $a;
  }
  $num_fds = sizeof($GLOBALS['fds']);
  $select='SELECT DISTINCT';
  $kwd = ' ';
  for ($k=0 ; $k<$num_fds; $k++) {
    $fd = $GLOBALS['fds'][$k];
    $select=$select.$kwd.fully_qualified_name($master_table,$field_defs,$fd,"change");
    $kwd = ',';
  }
  return $select;
}

function create_where_clause($master_table,$field_defs)
{
  while (list($a,$b) = each($field_defs)) {$GLOBALS['fds'][] = $a;}
  $num_fds = sizeof($GLOBALS['fds']);
  $kwd = ' WHERE (';
  for ($k=0 ; $k<$num_fds; $k++) {
      $fd = $GLOBALS['fds'][$k];
      if ((isset($field_defs[$fd]['values']['description'])) or (isset($field_defs[$fd]['values']['selectview']))) {
            $where = $where.$kwd.$master_table.'.'.$GLOBALS['fds'][$k].'='.
                    $field_defs[$fd]['values']['table'].'.'.$field_defs[$fd]['values']['column'].')';
            $kwd = ' AND (';
      }
  }
  return $where;
}

function create_from_clause($master_table,$field_defs)
{
  while (list($a,$b) = each($field_defs)) {$GLOBALS['fds'][] = $a;}
  $tbs[] = $master_table;
  for ($k=0 ; $k<sizeof($GLOBALS['fds']); $k++) {
    $fd = $GLOBALS['fds'][$k];
    if ((isset($field_defs[$fd]['values']['description'])) or (isset($field_defs[$fd]['values']['selectview']))) {
       if (!in_array($field_defs[$fd]['values']['table'],$tbs)) {
         $tbs[]=$field_defs[$fd]['values']['table'];
       }
    }
  }
  $kwd='';
  $from=' FROM ';
  for ($k=0; $k<sizeof($tbs); $k++) {
      $from .= $kwd.$tbs[$k];
      $kwd=',';
  }
  return $from;
}

?>
Return current item: phpMyAccess