phpとmysqlを使用して調査システムを作成しました。ユーザーから送信されたすべての回答を格納する「回答」と呼ばれる回答の表があります。一度に2つの質問の統計を表示するGoogleグラフを生成するスクリプトを作成しました。このスクリプトは、応答時間が非常に短い場合もあれば、永久にかかる場合もあり、内部サーバーエラーを返す場合もあります(結果を取得するのに時間がかかるためだと思いますが、よくわかりません)。
これがphpスクリプトです
<?
// DB CONFIG FILE
require("../db/config.inc.php");
// DB CLASS FILE
require("../db/Database.class.php");
// CREATE THE $DB OBJECT
$db = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
// DB CONNECTION
$db->connect();
error_reporting(-1);
// GET THE SERVER ID VIA POST
$sID = 1342625918; // HARD NUMBER FOR TESTING
// GET THE QUESTION IDS VIA POST
$question1 = 5; // HARD NUMBER FOR TESTING
$question2 = 17; // HARD NUMBER FOR TESTING
//GET THE NAME OF THE SURVEY
$sql = "SELECT * FROM survey WHERE sID = '$sID'";
$rows = $db->query($sql);
while ($record = $db->fetch_array($rows)) {
$sName = $record["sName"];
}
//GET QUESTION ONE
$sql = "SELECT * FROM questions WHERE qID = '$question1'";
$rows = $db->query($sql);
while ($record = $db->fetch_array($rows)) {
$q1Text = $record["qName"];
}
//GET QUESTION TWO
$sql = "SELECT * FROM questions WHERE qID = '$question2'";
$rows = $db->query($sql);
while ($record = $db->fetch_array($rows)) {
$q2Text = $record["qName"];
}
//THIS GETS THE NAMES FOR THE GOOGLE GRAPH
$sql006 = "SELECT * from survey_selected_question, survey_selected_question_selected_option, options WHERE survey_selected_question.qID = '$question1' AND survey_selected_question_selected_option.ssqID = survey_selected_question.ssqID AND options.oID = survey_selected_question_selected_option.oID AND survey_selected_question.sID = '$sID'";
$rows006 = $db->query($sql006);
$numRes006 = $db->affected_rows;
$names = "";
while ($record006 = $db->fetch_array($rows006)) {
$names .= "'".$record006["oName"]."',";
}
// CLEAN UP THE NAMES VAR
$names = substr($names, 0, -1);
// START FUNCTION FOO
function foo($answer1, $answer2, $sID){
// CREATE THE DB OBJECT
$dbOne = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
// DB CONNECTION
$dbOne->connect();
$returnVal = 0;
//GET THE DISTINCT SUBMISSIONS FROM THE ANSWERS TABLE FOR THIS SPECIFIC SURVEY
$sql0 = "SELECT DISTINCT(submissionID) FROM answer WHERE sID = '$sID'";
$rows0 = $dbOne->query($sql0);
while ($record0 = $dbOne->fetch_array($rows0)) {
$submissionID = $record0["submissionID"];
// SELECT SUBMISSIONS WHERE ANSWER = PASSED ANSWER 1 or 2
$sql01 = "SELECT * from answer WHERE submissionID = '$submissionID' AND (answer = '$answer1' OR answer = '$answer2')";
$rows01 = $dbOne->query($sql01);
$numRes = $dbOne->affected_rows;
if($numRes=='2'){
$returnVal = $returnVal + 1;
}
}
return $returnVal;
$dbOne->close();
}
//END FUNCTION FOO
// START FUNCTION FOO2
function foo2($oID, $sID, $question1){
// CREATE THE DB OBJECT
$dbTwo = new Database(DB_SERVER, DB_USER, DB_PASS, DB_DATABASE);
// DB CONNECTION
$dbTwo->connect();
// GET OPTION IDS AND BUILD AN OUTWARD STRING FOR GOOGLE GRAPH OF RESULTS
$sql2 = "SELECT * from survey_selected_question, survey_selected_question_selected_option, options WHERE survey_selected_question.qID = '$question1' AND survey_selected_question_selected_option.ssqID = survey_selected_question.ssqID AND options.oID = survey_selected_question_selected_option.oID AND survey_selected_question.sID = '$sID'";
$rows2 = $dbTwo->query($sql2);
$returnVal2 = "";
while ($record2 = $dbTwo->fetch_array($rows2)) {
$returnVal2 .= "".foo($record2["oID"], $oID, $sID).",";
}
//END FUNCTION FOO2
// CLEAN UP THE RETURNVAL2
substr($returnVal2, 0, -1);
// RETURN THE VAL HERE
return $returnVal2;
$dbTwo->close();
}
// START BUILDING THE google Array
$sql003 = "SELECT * from survey_selected_question, survey_selected_question_selected_option, options WHERE survey_selected_question.qID = '$question2' AND survey_selected_question_selected_option.ssqID = survey_selected_question.ssqID AND options.oID = survey_selected_question_selected_option.oID AND survey_selected_question.sID = '$sID'";
$rows003 = $db->query($sql003);
$numRes2 = $db->affected_rows;
$googleArray = "";
while ($record003 = $db->fetch_array($rows003)) {
$googleArray .= "['".$record003["oName"]."',".foo2($record003["oID"], $sID, $question1)."],";
?>
<?
}
// CLEAN UP THE GOOGLE ARRAY
$googleArray = substr($googleArray, 0, -1);
?>
<!DOCTYPE HTML>
<html>
<head>
<meta name = "viewport" content = "width = device-width">
<meta name = "viewport" content = "initial-scale = 1.0, user-scalable = no">
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Name',<?= $names; ?>],
<?= $googleArray; ?>
]);
var options = {
title: 'Line graph for question "<?= $q1Text; ?>" against question "<?= $q2Text; ?>"'
, colors:['#5D0F99','#2F543B','#BD2115','#D67314','#F92985','#0D85C7','#000D9C','#D9A5F6', '#27E80C', '#725F53']
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<style>
div#wrapper {
max-width: 700px;
overflow:hidden;
background:#eee;
margin: 0 auto;
padding: 8px 8px 8px 8px;
border:1px solid black;
}
a{
color:#000;
}
input.goback {
background-color: #ccc;
border: 1px outset #000;
color: #000;
padding: 4px;
margin-left: 2px;
}
</style>
</head>
<body>
<div id="wrapper">
<h1><?= $sName; ?></h1>
<form method="post" action="main.php">
<p>Show me average(s) on
<select name="oID">
<?
//GET QUESTION ONE
$sql004 = "SELECT * from survey_selected_question, survey_selected_question_selected_option, options WHERE survey_selected_question.qID = '$question1' AND survey_selected_question_selected_option.ssqID = survey_selected_question.ssqID AND options.oID = survey_selected_question_selected_option.oID AND survey_selected_question.sID = '$sID'";
$rows004 = $db->query($sql004);
while ($record004 = $db->fetch_array($rows004)) {
?>
<option value="<?= $record004["oID"]; ?>"><?= $record004["oName"]; ?></option>
<?
}
?>
</select>
with these survey type(s):
<select name="types">
<option value="0">All</option>
<?
//GET THIS SURVEYS TYPES
$sql005 = "SELECT * from survey_types, survey_selected_types WHERE survey_selected_types.sID = '$sID' AND survey_selected_types.stID = survey_types.stID";
$rows005 = $db->query($sql005);
while ($record005 = $db->fetch_array($rows005)) {
?>
<option value="<?= $record005["stID"]; ?>"><?= $record005["stName"]; ?></option>
<?
}
?>
</select>
over a 12 month period.
<input type="hidden" name="sID" value="<?= $sID; ?>">
<input type="hidden" name="question1" value="<?= $question1; ?>">
<input type="hidden" name="edit" value="average">
<input type="hidden" name="question2" value="<?= $question2; ?>">
<input type="submit" name="submit" value="Go" class="goback">
</p>
</form>
<div id="chart_div" style="width: 675px; height: 500px;"></div>
<form method="post" action="main.php">
<p>
<input type="hidden" name="edit" value="survey">
<input type="hidden" name="sID" value="<?= $sID; ?>">
<input type="submit" value="Back To Statistics" class="goback">
</p>
</form>
</div>
</body>
</html>
<?
$db->close();
?>
そしてここにそれが出力するhtmlがあります(それが機能する場合...私が言ったように、それはかなり速く戻ることもあれば、内部サーバーエラーでまったく返さないこともあります)
<!DOCTYPE HTML>
<html>
<head>
<meta name = "viewport" content = "width = device-width">
<meta name = "viewport" content = "initial-scale = 1.0, user-scalable = no">
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["corechart"]});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Name','None','$1 - $50','$51 - $200','Over $200'],
['Yes',85,100,87,82,],['No',70,82,81,74,],['Undecided ',76,90,90,84,]]);
var options = {
title: 'Line graph for question "Not including this event, please estimate how much will you spend in the community during your visit, including accommodations, transportation, food, and gas, etc.)" against question "Would you describe this event as providing "good value?"'
, colors:['#5D0F99','#2F543B','#BD2115','#D67314','#F92985','#0D85C7','#000D9C','#D9A5F6', '#27E80C', '#725F53']
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
<style>
div#wrapper {
max-width: 700px;
overflow:hidden;
background:#eee;
margin: 0 auto;
padding: 8px 8px 8px 8px;
border:1px solid black;
}
a{
color:#000;
}
input.goback {
background-color: #ccc;
border: 1px outset #000;
color: #000;
padding: 4px;
margin-left: 2px;
}
</style>
</head>
<body>
<div id="wrapper">
<h1>Test Survey One</h1>
<form method="post" action="main.php">
<p>Show me average(s) on
<select name="oID">
<option value="33">None</option>
<option value="34">$1 - $50</option>
<option value="35">$51 - $200</option>
<option value="36">Over $200</option>
</select>
with these survey type(s):
<select name="types">
<option value="0">All</option>
<option value="13">One week</option>
<option value="16">Paid</option>
<option value="18">Fair</option>
<option value="50">Live performance(s)</option>
<option value="51">Exhibition</option>
<option value="59">Community Pride</option>
<option value="54">Local foods / goods (grown or produced)</option>
<option value="53">Agriculture</option>
<option value="52">Art and Culture</option>
<option value="61">Industry</option>
<option value="62">Shop local</option>
<option value="39">Simcoe</option>
<option value="48">Fall</option>
<option value="65">Sale(s)</option>
<option value="66">Vendors</option>
<option value="67">Midway / rides</option>
<option value="68">Live music</option>
<option value="70">Live performance (not music or theatre)</option>
</select>
over a 12 month period.
<input type="hidden" name="sID" value="1342625918">
<input type="hidden" name="question1" value="5">
<input type="hidden" name="edit" value="average">
<input type="hidden" name="question2" value="17">
<input type="submit" name="submit" value="Go" class="goback">
</p>
</form>
<div id="chart_div" style="width: 675px; height: 500px;"></div>
<form method="post" action="main.php">
<p>
<input type="hidden" name="edit" value="survey">
<input type="hidden" name="sID" value="1342625918">
<input type="submit" value="Back To Statistics" class="goback">
</p>
</form>
</div>
</body>
</html>
なぜこれが時々すぐに機能し、他の人は基本的にタイムアウトか何かのように見えるのかについての考えはありますか?
注:初めてquestion1とquestion2の番号を渡してページを読み込むと、タイムアウトになります。更新を押すとロードされます