I just hit a point in an app I am currently developing, where I found myself generating queries from the resultset of another query.

Because, what can possibly go wrong, right?

$selectors = array();
$query_phase2 = "SHOW KEYS FROM `".$row['table']."` WHERE Key_name = 'PRIMARY'";
$query_phase3 = "select `".$row['field']."` from `".$row['table']."` where ";
if($result = $GLOBALS['dbconn']->query($query_phase2)) { 
    while ($row = $result->fetch_assoc()) {
        if(array_key_exists($row['Column_name'], $data)) {
            $qextender = "`".$row['Column_name']."` = '".$data[$row['Column_name']]."'";
            array_push($selectors,$qextender);
        }
    }
    $query_phase3 .= implode(" and ",$selectors);
    if($result = $GLOBALS['dbconn']->query($query_phase3)) { 
        if($result->num_rows > 1) {
            $return = array();
            while($row = $result->fetch_array()) {
                array_push($return,$row[0]);
            }
        } elseif ( $result->num_rows == 1) {
            $return = $result->fetch_array();
            return $return[0];
        } else {
            return "";
        }
    }
}else { return array('danger' => 'query failed('.$GLOBALS['dbconn']->errno.'): '.$GLOBALS['dbconn']->error);}