I do a query to MySQL using PHP. Well actually I do too many of them
which is the problem, it's too slow. I think maybe an inner join or
something would be better but I can't figure this out.
The web page on which the data is displayed has a list of countries.
Each country has a label heading under which websites are listed that
have information on that country. Any given website may appear many
times (under different countries) if it has information on that country.
I've got three tables. They are:
country - A list of countries and their id key.
data_pairs - Country and website id pairs
data - A list of websites and their id key.
I do a query to get all the countries, then a while loop to print a
heading for each country. Inside this while loop is another query and
while loop to get all the website id's that have information on that
country. Inside this while loop is another query and while loop to
get the websites and print them.
So to produce a medium size web page I'm doing like fifty queries!
It takes a few seconds, I'm surprised it works at all. Is there a
way to do all of this in one fell swoop, one query?
Thanks for your help.
The PHP code is listed below:
-------------------------------------------------------------
function GetData()
{
$link = $this->MakeConn();
mysql_select_db ("gdk") or die("Could not select database");
// Get all the countries (id, country FROM country table)
$query = "SELECT * FROM country ORDER BY country";
$result = mysql_query($qu ery) or die("Query failed : " . mysql_error());
// Iterate thru the countries and print their heading label
while ($row = mysql_fetch_arr ay($result, MYSQL_NUM))
{
$typeNum = $row[0]; // the country id
printf ("<div class=\"Box\">% s</div>", $row[1]); // print the heading
// get the website id's for this one country
// (site_id, country_id, alpha FROM data_pairs table)
$query2 = "SELECT * FROM data_pairs WHERE country_id = $typeNum ORDER BY alpha";
$result2 = mysql_query($qu ery2) or die("Query failed : " . mysql_error());
/* I now have all the site id's for this one country.
Do query to get the websites. Do one query for each
country because the sites have nothing in common.
*/
// Iterate thru the site id's and print them.
while ($row2 = mysql_fetch_arr ay($result2, MYSQL_NUM))
{
// Get a link (for this country)
$num = $row2[0];
// Get the site row (id, site_name, site_URL FROM data table)
$query3 = "SELECT * FROM data WHERE id = $num";
$result3 = mysql_query($qu ery3) or die("Query failed : " . mysql_error());
$site = mysql_fetch_arr ay($result3, MYSQL_NUM);
printf("<a href=\"$site[2]\" target=\"_blank \">$site[1]</a></br>");
}
printf("<br>");
} //while
// Free resultset
mysql_free_resu lt($result);
// Close connection
mysql_close($li nk);
} //function
which is the problem, it's too slow. I think maybe an inner join or
something would be better but I can't figure this out.
The web page on which the data is displayed has a list of countries.
Each country has a label heading under which websites are listed that
have information on that country. Any given website may appear many
times (under different countries) if it has information on that country.
I've got three tables. They are:
country - A list of countries and their id key.
data_pairs - Country and website id pairs
data - A list of websites and their id key.
I do a query to get all the countries, then a while loop to print a
heading for each country. Inside this while loop is another query and
while loop to get all the website id's that have information on that
country. Inside this while loop is another query and while loop to
get the websites and print them.
So to produce a medium size web page I'm doing like fifty queries!
It takes a few seconds, I'm surprised it works at all. Is there a
way to do all of this in one fell swoop, one query?
Thanks for your help.
The PHP code is listed below:
-------------------------------------------------------------
function GetData()
{
$link = $this->MakeConn();
mysql_select_db ("gdk") or die("Could not select database");
// Get all the countries (id, country FROM country table)
$query = "SELECT * FROM country ORDER BY country";
$result = mysql_query($qu ery) or die("Query failed : " . mysql_error());
// Iterate thru the countries and print their heading label
while ($row = mysql_fetch_arr ay($result, MYSQL_NUM))
{
$typeNum = $row[0]; // the country id
printf ("<div class=\"Box\">% s</div>", $row[1]); // print the heading
// get the website id's for this one country
// (site_id, country_id, alpha FROM data_pairs table)
$query2 = "SELECT * FROM data_pairs WHERE country_id = $typeNum ORDER BY alpha";
$result2 = mysql_query($qu ery2) or die("Query failed : " . mysql_error());
/* I now have all the site id's for this one country.
Do query to get the websites. Do one query for each
country because the sites have nothing in common.
*/
// Iterate thru the site id's and print them.
while ($row2 = mysql_fetch_arr ay($result2, MYSQL_NUM))
{
// Get a link (for this country)
$num = $row2[0];
// Get the site row (id, site_name, site_URL FROM data table)
$query3 = "SELECT * FROM data WHERE id = $num";
$result3 = mysql_query($qu ery3) or die("Query failed : " . mysql_error());
$site = mysql_fetch_arr ay($result3, MYSQL_NUM);
printf("<a href=\"$site[2]\" target=\"_blank \">$site[1]</a></br>");
}
printf("<br>");
} //while
// Free resultset
mysql_free_resu lt($result);
// Close connection
mysql_close($li nk);
} //function
Comment