M
M
machno2014-06-05 13:45:09
PHP
machno, 2014-06-05 13:45:09

How to generate output from MySQL when there are more than one queries?

There are several tables in the database. At the moment, I take out one query from 2 tables and display it like this:

while($row = mysql_fetch_array($result))
{
  echo "ID: ".$row['id_product']." ";
  echo "Имя: ".$row['name']."<br>";
  echo "Цена закупочная: ".$row['wholesale_price']."<br>";
  echo "Цена продажи: ".$row['price']."<br>";
        $list = array ($name.$wholesale_price.';'.$price.';'.$profit);
  foreach ($list as $line) {
    fputcsv($fp, explode(';', $line));
  }
}

Well, then I also enter all the goodness received into a .csv file.
I would like to get several arrays for several requests from the database (one request - one array).
And then output all this happiness to the browser and to a .csv file

Answer the question

In order to leave comments, you need to log in

3 answer(s)
E
Edward, 2014-06-05
@evsmusic

$queries = [
    'cars'=>'SELECT some FROM cars',
    'users'=>'SELECT some FROM users'
];

$results = [
    'cars'=>[],
    'users'=>[],
];

foreach($queries AS $name=>$query) {
    //тут выполнение запроса и далее как у Вас:
    while($row = mysql_fetch_array($result)) {
        $results[$name][] = $row;
    }
}

PS: and stop using MySQL API, use PDO

I
Ilya Lesnykh, 2014-06-05
@Aliance

$list = array();
while($row = mysql_fetch_array($result))
{
  echo "ID: ".$row['id_product']." ";
  echo "Имя: ".$row['name']."<br>";
  echo "Цена закупочная: ".$row['wholesale_price']."<br>";
  echo "Цена продажи: ".$row['price']."<br>";
        $list[] = $name.$wholesale_price.';'.$price.';'.$profit;
}
while (another_query) {
        // ...
        $list[] = '...';
}
// ...
foreach ($list as $line) {
        fputcsv($fp, explode(';', $line));
}

E
evnuh, 2014-06-05
@evnuh

Well, do 2 requests and for each save the result to your own array, what's wrong?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question