So today I jumbled together a script that would get data from a database, from different tables and such. I had wanted to just use simple ifs without the whiles, but I couldn't make it possible. The code I posted is useable but randomly times out when testing, and i'm afraid of how it would perform under stress. Is there any way to improve this code to stop the timing out?
<?php
$getConference = sprintf("SELECT DISTINCT confName, tableName FROM conferences WHERE enabled='1' ORDER BY id DESC");
$catConference = mysql_query($getConference);
while($conference = mysql_fetch_array($catConference)) {
echo "<h2 class=\"expand\">".$conference['confName']."</h2>
<ul class=\"collapse\">";
$getExistingEvent = sprintf("SELECT DISTINCT event FROM " . $conference['tableName']);
$catExistingEvent = mysql_query($getExistingEvent);
while($existingevent = mysql_fetch_array($catExistingEvent)) {
$getCat = sprintf("SELECT * FROM event WHERE id='".$existingevent['event']."'");
$catResult = mysql_query($getCat);
while($row = mysql_fetch_array($catResult)) {
$getWinner = sprintf("SELECT place, name, event FROM 2012rlc WHERE event='".$row['id']."' ORDER BY place");
$catWinner = mysql_query($getWinner);
echo "
<li>
<h2 class=\"expand\">".$row['eventName']."</h2>
<ul class=\"collapse\">
";
while($winner = mysql_fetch_array($catWinner)) {
echo "<li>".$winner['place']. " ".$winner['name']."</li>";
}
echo "</ul></li>";
}
}
echo "</ul>";
}
mysql_free_result($catResult);
mysql_free_result($catWinner);
mysql_free_result($catExistingEvent);
mysql_free_result($catConference);
?>
Some have asked for the database schema, which I have included below:
CREATE TABLE `2012rlc` (
`id` int(11) unsigned NOT NULL auto_increment,
`place` varchar(4) default '',
`name` varchar(255) default NULL,
`event` int(5) default NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=52 DEFAULT CHARSET=latin1;
CREATE TABLE `conferences` (
`id` int(11) unsigned NOT NULL auto_increment,
`confName` varchar(255) default NULL,
`tableName` varchar(50) default NULL,
`enabled` tinyint(1) default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;
CREATE TABLE `event` (
`id` int(11) unsigned NOT NULL auto_increment,
`eventName` varchar(255) default NULL,
`teamEvent` tinyint(1) default '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=47 DEFAULT CHARSET=latin1;
Output to EXPLAIN MySQL command:
id select_type table type possible_keys key key_len ref rows Extra
1 SIMPLE conferences ALL NULL NULL NULL NULL 4 Using where; Using temporary; Using filesort
conferences.confName
and2012rlc.name
? Is the schema in 3NF? – palacsint Dec 19 '11 at 22:21