Nested While Loops php

I am trying to get a series of nested loops that work to select a database name from a single table, then query the selected table in that database and add the results and display their number and database name.

I got the code to work, but it continues to show:

Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in

I tried my best, I found online to help, but no one works.

$resulta = mysql_query("SELECT dbname AF012 FROM Customer");

while($data = mysql_fetch_array($resulta))
  {
    $db = $data[' dbname '];

$result = null;
$result2 = mysql_query("SELECT changemade FROM $db.orders");
//looping through the results
while($row = mysql_fetch_array($result2))
  {
    //checking if any record is 1,2 or 3
  if( ($row[‘changemade’]== 1) || ($row[‘changemade’]== 2) || ($row[‘changemade’]== 3) )          {
      //if any match the if adding 1 to the counter
      $counter ++;
    }
     }
 unset($result2);
echo $db."  ".$counter;
 echo "<br>";
 $counter = 0;
 $result = null;
 $result2 = null;
}

All database connections are created and work fine, so it has nothing to do with it. Any help would be great.

+3
source share
2 answers

, . , , :

function mysql_query_array($query)
{
    if (!$result = mysql_query($query)) {
        throw new Exception(sprintf('Invalid query "%s", error: %s.', $query, mysql_error()));
    }
    return function () use ($result) {
        return mysql_fetch_array($result);
    };
}

:

$queryA = mysql_query_array("SELECT dbname AF012 FROM Customer");
while ($data = $queryA())
{
    $counter = 0;
    $queryB = mysql_query_array("SELECT changemade FROM {$data['dbname']}.orders");
    while ($row = $queryB())
    {
        if (in_array($row['changemade'], range(1, 3))) {
            $counter++;
        }
    }
    printf("%s  %s<br>\n", $db, $counter);
}
+4

, mysql_query false, , , mysql_fetch_array() . $db = $data[' dbname ']; $db = $data['dbname'];

+1

All Articles