0

I've looked for something similar on stack but nothing exactly as this.

I (think I) need to generate a unique MySQL query inside a loop as each iteration needs to look up a different table. the loop is from an exploded $_GET array.

The problem is creating a differently named mysql query based on the loop iteration. I've done it where the $var name is different but it doesn't work, I think because it is a string not a variable?

Any help appreciated

$temps = explode(",", $_GET['temps']);
$tempCount = count($temps); 

for ($i=0; $i<$tempCount; $i++)
{
/*'normal' database lookup
$check = mysql_query("SELECT * FROM _db_".$temps[$i]."");       
    $checks = array();
    while ($row = mysql_fetch_assoc($check)) {
    $checks[] = $row;
    }*/


//here's where I'm trying to build a 'dynamic' lookup for each loop iteration

$checkTemp=$check.$temps[$i];
$checkTempArray=$check.$temps[$i].'Array';

$checkTemp = mysql_query("SELECT * FROM _db_".$temps[$i]."");       
    $checkTempArray = array();
    while ($row = mysql_fetch_assoc($checkTemp)) {
    $checkTempArray[] = $row;
    }
}
5
  • $check is not being used Commented Jul 10, 2013 at 11:15
  • What do you mean by a 'differently named' MySQL query? Commented Jul 10, 2013 at 11:15
  • I've amended it so you can see that the db table name is different for each iteration. So the array that needs to be stored has to have a different name for later lookup (foreach,etc). Eg arrays $checkJohn and $checkSally Commented Jul 10, 2013 at 11:18
  • $checkTemp = mysql_query("SELECT * FROM db".$temp[$i].""); either use $temps[$i] or just $temp, $temp[$i] doesn't makes any sense Commented Jul 10, 2013 at 11:21
  • Thanks, I've amended that. But I need to create a dynamically created array, too, for later lookup. That's my main problem Commented Jul 10, 2013 at 11:26

2 Answers 2

1

If I understand correctly you're trying to SELECT * from all tables seperated by , in the $_GET["temps"]

$temps = explode(",", $_GET['temps']);
$tempCount = count($temps); 

$allResults = array();
for ($i=0; $i<$tempCount; $i++)
{
    $checkTemp = mysql_query("SELECT * FROM _db_".mysql_real_escape_string($temps[$i]));
    $allResults[$temps[$i]] = array();
    while ($row = mysql_fetch_assoc($checkTemp))
    {
        $allResults[$temps[$i]][] = $row;
    }
}
// Now for example $allResults["john"][3] contains the fourth row in the table _db_john
print_r($allResults["sally"][2]); // print the third row in _db_sally
2
  • Hi. I've amended my code to show the _db appended with the temp variable. so the url '&temps=john,sally' will look up _db_john and _db_sally Commented Jul 10, 2013 at 11:14
  • @gavin I changed my answer, it now stores the results in arrays. Commented Jul 10, 2013 at 11:24
0

Seems like a typo in your code

$checkTemp = mysql_query("SELECT * FROM db".$temp[$i].""); 

either use

$temps[$i] or just  $temp 
$temp[$i] doesn't makes any sense

so your query should be instead

$checkTemp = mysql_query("SELECT * FROM db".$temps[$i]."");  

EDIT: for your array part you can use

 $$temp = array();
 while ($row = mysql_fetch_assoc($checkTemp)) {
    $$temp[] = $row;
 }
2
  • Thanks,boom_Shiva. I've amended that now. I need to create a dynamically created array, too, for later lookup. That doesn't seem to work Commented Jul 10, 2013 at 11:28
  • I've simplified it, just using $temps[i]. Commented Jul 10, 2013 at 11:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.