Jump to content

[SOLVED] Timed Out ... How do I make this simple??


Gem

Recommended Posts

Hiya ...

 

I need a script that does this .. but doesn't time out.

 

I knew it wouldn't like it but I dont know how to do it so thought I'd give it ago anyway and see what happens ...

 

Any ideas??

 

January
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-01-01'
and date '2009-01-31';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>
February
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-02-01'
and date '2009-02-28';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>
March
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-03-01'
and date '2009-03-31';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>
April
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-04-01'
and date '2009-04-30';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>
May
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-05-01'
and date '2009-05-31';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>
June
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-06-01'
and date '2009-06-30';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>
July
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-07-01'
and date '2009-07-31';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>
August
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-08-01'
and date '2009-08-31';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>
September
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-09-01'
and date '2009-09-30';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>
October
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-10-01'
and date '2009-10-31';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>
November
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-11-01'
and date '2009-11-30';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>
December
<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);$result = mysql_query(
"select * from diary 
where date between date '2009-12-01'
and date '2009-12-31';");while($row = mysql_fetch_array($result))
  {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }mysql_close($con);
?>

 

Thanks

 

X

There are several problems I see with the code.

 

First, of course, is that it all can be combined into a single statement since there is so much repetitive code.

 

This has not been tested, so I offer it up as is.  I will probably add to my signature that the code is never tested by me. hehe

 

<?php
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
// Set month as an array.  Keys are 1-12 (January = 0, December = 12)
$month = array("1" => "January", "2" => "February", "3" => "March", "4" => "April", "5" => "May", "6" => "June", "7" => "July", "8" => "August", "9" => "September", "10" => "October", "11" => "November", "12" => "December");

mysql_select_db("bssql", $con);
for($i = 0; $i <= 12; $i++)
  {
echo "$month[$i]"; // Should display the proper name of the month for each entry.
if ($i < 10) { $i = "0" + $i; } // add a 0 in front if the month is less than 10
$result = mysql_query("SELECT * FROM diary WHERE date BETWEEN date '2009-" . $i . "-01'
AND date '2009-" . $i . "-31'");
while($row = mysql_fetch_assoc($result)) {
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
  }
// Here you would add what you want to be between each of the months.  I'm
// leaving this out because I don't know what your layout is suppose to look like.
}
?>

 

A) Connect to the database server and select the database once at the start of your code. Don't connect/close the connection 12 times.

 

B) Form one query that selects all the data you want and orders it the way you want.

 

C) Add a term to your SELECT statement to get the month name using the mysql MONTHNAME() function.

 

D) Simply have your php application code detect a change in the month name to start a new section of the output with the month name as the heading.

 

E) Don't put queries inside of loops.

 

Code to do the above (untested) -

 

<?php
// assumes all the data for the current year is to be retrieved
$con = mysql_connect("80.94.196.33","gem","landseer");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
}
mysql_select_db("bssql", $con);

$result = mysql_query(
"select *,MONTHNAME(date) as mn from diary 
where year(date) = year(curdate()) order by date");

$previous_month = ""; // initialize variable to detect change in month name
while($row = mysql_fetch_array($result))
{
if($row['mn'] != $previous_month){
	echo $row['mn'] . '<br />'; // display the month name
	$previous_month = $row['mn'];
}
  ?><H2><?php echo nl2br($row['event']); ?> </H2>
    <H3><?php echo nl2br($row['date']); ?> </H3>
        <?php echo nl2br($row['details']);
              echo "<br />##########################################";
              echo "<br /><br />";
}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.