Sorry in advance for the complex question. i've tried to simplify it as much as possible to get to the root of my question.
I'm having trouble with the length of time it takes the below script to get data from a mysql database. I know there's probably a simpler way to query the database to speed things up but i can't figure it out. basically i'm querying 3 tables and printing out a page with multiple rows of info. the link to the page that the visitor clicks on has a variable attached to it using the GET method like this:
www.mywebpage.com/test.php?projectID=12345
Then the php queries the database like this:
<?
//[....make database connection....]
//get first chunk of info based on the variable in the weblink
$result = mysql_query("SELECT table1.viewerID, table2.email, max(table1.timeout), count(table1.viewerID)
FROM table1, table2 WHERE projectID=$projectID");
//this WHILE loop will print out multiple rows of info based on the above query
//and also the query inside the FOREACH loop below
while ($row = mysql_fetch_array($result))
{
$id = $row[0];
$email = $row[1];
$datetime = $row[2];
$visits = $row[3];
print "$datetime, $email, $visits, ";
//the "$events" variable below is a dynamic array that is carried over from a previous session page.
//I have written it out here for the purpose of clarity.
$events=array("event1", "event2", "event3");
foreach ($events as $value)
{
$result2 = mysql_query("SELECT columnA FROM table3 WHERE columnB=$value
and columnC=$id");
$rowB = mysql_fetch_array($result2);
print "$rowB[0], ";
}
print"<br>";
}
//[...close database connection...]
?>
The end result is a page with multiple rows like this:
Jan 10 , someone@email.com, 3, 27, 12, 57
Jan 08 , somebody@email.net, 55, 42, 7, 101
It works, but once i start getting into higher numbers of rows (above a few hundred) it starts taking a VERY long time to return data. It takes over 2 minutes for 1000 rows with 10 columns each. My guess is that the FOREACH loop inside the WHILE loop is not the best way to go about this but it's the only way i could think of to accomplish what i needed since the quantity of data in the "$events" array is always changing. It could be 3 events or 10 or whatever. I have similar queries that don't have a FOREACH loop inside the WHILE loop, and they seem to retrieve similar amounts of data much faster. Is there a better way to formulate these queries to get faster results? Thanks, Dave