# Querying a query?

**URL:** https://forum.kirupa.com/t/querying-a-query/268662
**Category:** programming
**Created:** [August 16, 2008, 11:25pm UTC](https://forum.kirupa.com/t/querying-a-query/268662 "2008-08-16T23:25:52Z")
**Posts on this page:** 1
**Page:** 1

<div class="post-metadata">

### Author: ![Sage\_of\_Fire](https://avatars.discourse-cdn.com/v4/letter/s/e95f7d/32.png) [@Sage\_of\_Fire](https://forum.kirupa.com/u/Sage_of_Fire)
#### Post date: [August 16, 2008, 11:25pm UTC](https://forum.kirupa.com/t/querying-a-query/268662/1 "2008-08-16T23:25:52Z")

</div>

I’m just getting started with the whole php/mysql interaction, but I still don’t want to make a second-rate script just because I don’t know a better way. I built a standard, ugly procedural script for reading from a standard, ugly mysql database. I want to know how I can do one broad query, use one row of it one place, and the rest somewhere else.

I DO NOT want to query the database twice. I just need to know how to access a specific row of a broad query result. Here’s the relevant code:

```auto

if ( isset($_GET['some_column']) ) {

  $q = 'SELECT * FROM table WHERE some_column = ' . $_GET['some_column'];

  $r = mysqli_query ($dbc, $q);
  
  $row = mysqli_fetch_array( $r, MYSQLI_ASSOC );
  
  foreach ($row as $key => $value) {

// do stuf with the values...

  } // end FOR EACH
}

$q = 'SELECT * FROM inventory ORDER BY truck_num';

$r = mysqli_query ($dbc, $q);

while ( $row = mysqli_fetch_array($r, MYSQLI_ASSOC) ) {
  
  foreach ($row as $key => $value) {

  // do stuff with keys and values...

  } // end FOREACH
  
} // end WHILE

```

I’m sure there’s some way to get the information from the top query from the bottom query. Can someone help me simplify this code? (no OOP, please)
