Simplifique 1000 consultas SQL SELECT en PHP en un bucle

How would this be looped in the shortest way:

$db->setQuery("SELECT val1 FROM table1 WHERE val2 = 'AAA'");
$row1 = $db->loadObjectList();
$var1 = $row1[0];

$db->setQuery("SELECT val3 FROM table1 WHERE val2 = 'AAA'");
$row2 = $db->loadObjectList();
$var2 = $row2[0];

$db->setQuery("SELECT val1 FROM table1 WHERE val2 = 'BBB'");
$row3 = $db->loadObjectList();
$var3 = $row3[0];

$db->setQuery("SELECT val3 FROM table1 WHERE val2 = 'BBB'");
$row4 = $db->loadObjectList();
$var4 = $row4[0];

...

$db->setQuery("SELECT val1 FROM table1 WHERE val2 = 'AAA'");
$row997 = $db->loadObjectList();
$var997 = $row997[0];

$db->setQuery("SELECT val3 FROM table1 WHERE val2 = 'AAA'");
$row998 = $db->loadObjectList();
$var998 = $row998[0];

$db->setQuery("SELECT val1 FROM table1 WHERE val2 = 'BBB'");
$row999 = $db->loadObjectList();
$var999 = $row999[0];

$db->setQuery("SELECT val3 FROM table1 WHERE val2 = 'BBB'");
$row1000 = $db->loadObjectList();
$var1000 = $row1000[0];

I have 1000 of these queries and I am stuck on putting a loop around them.

Also, do I need to declare all global variables one by one, i.e. $var1, $var2, ... var1000 or could this be looped as well?

preguntado el 05 de mayo de 13 a las 12:05

Is your intended end result that you have 1000 variables of $rowX and 1000 variables of $varX, o es $rowX just intermediate for each case (and can be thrown away)? -

$rowX is just intermediate -

Great - see @Yogesh's answer. Since $row is intermediate, he's re-used the same variable for this throughout the loop, rather than having a new one for each intermediate result. -

When I saw this code all I could think of was sending a link to TheDailyWTF. -

It's not code yet, it's only the description of the task written in PHP/SQL. Now you made me wasting time on that site. But thanks for the hint anyways. -

2 Respuestas

Create array of your queries and iterate like this

$var = array(); // storing result in this array
$array_qry = array("SELECT val1 FROM table1 WHERE val2 = 'AAA'","SELECT val3 FROM table1 WHERE val2 = 'AAA'");// array of query
foreach($array_qry as $qry) {
    $db->setQuery($qry);
    $row = $db->loadObjectList();
    $var[] = $row[0];
}

y luego iterar $var for getting stored values

contestado el 05 de mayo de 13 a las 12:05

Thanks, sounds good - I will try and post result later. One part of the question which seems not covered is looping through AAA and BBB of the SQL queries itself. val2 is alternating throughout the queries. - user2317194

@user2317194 store your queries here $array_qry = array(); - Yogesh Suthar

Yes, that will put the PHP part into a loop. But the SQL part looks basically like this: SELECT val1 FROM table1 WHERE val2 = 'AAA' SELECT val3 FROM table1 WHERE val2 = 'AAA' SELECT val1 FROM table1 WHERE val2 = 'BBB' SELECT val3 FROM table1 WHERE val2 = 'BBB' Could this be looped, too? I just edited the question in order to reflect that better ... - user2317194

Normally, I'd expect to see you execute SELECT val1, val3, FROM Table1 WHERE val2='AAA';. Then load one set of objects from the column "val1", and another set of objects from the column "val3". You can actually recuperar all the data in one query: SELECT val1, val3, 'AAA' FROM Table1 WHERE val2 = 'AAA' UNION ALL SELECT val1, val3, 'BBB' FROM Table1 WHERE val2 = 'BBB';, but your PHP gets a little more complicated. - Mike Sherrill 'Cat Recall'

Thanks for the building blocks to the answer. - user2317194

$resultArray = array();
$queryParam = array('AAA', 'BBB', 'CCC');  // Add Query param value
$fieldArray = array('val1', 'val3','val4')  // Add field attributes
foreach($queryParam as $qryPrm) {
    foreach($fieldArray as $field) {
        $sql= "select ". $field . " from mytable where val2 = '".$qryPrm."'";
        $db->setQuery($qry);
        $row = $db->loadObjectList();
        $resultArray[] = $row[0];
    }
}   

¿Esto funciona para tí?

contestado el 05 de mayo de 13 a las 20:05

Yes. Thanks a million. So easy. - user2317194

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.