Jump to content

php script not completing


themace

Recommended Posts

Hi Everyone,

While this is my first post I have been programming php for some 4 years now and am completely puzzled as to why my php script halts half way through the execution.  It is not a logic error and all I can assume is that it is the the fact that I am using arrays quite alot.

The weird thing about it is that it halts at different parts and in firefox at least it delivers between 1/2 a page and 3/4 a page.  If I limit the top query to 0,10 (from 19) it completes just fine.

I was wondering if there was any reason as to why php might do this. The script doesn't take particularly long and I have even tried setting the time limit to 0 using set_time_limit(0).  The only thing I can think of is memory.

If anyone could help in any way it would be much appreciated.

Kind Regards,
themace
Link to comment
Share on other sites

<?php

$city_query = "SELECT c.cityid, c.city, COUNT(u.cityid) AS count FROM cities c LEFT OUTER JOIN user_city u ON u.cityid = c.cityid GROUP BY c.cityid ORDER BY c.city ASC";
//Taking about 0.0067 seconds and runs once

$city_query = mysql_query($city_query);
while($row = mysql_fetch_array($city_query))
{
//INitially put this in a multidimentional array but thought that the memory leak mentioned here:
//http://bugs.php.net/bug.php?id=37929
//May have been the cause

//NO NULL values returned
//Now they are in idividual arrays
$cities[] = $row['city'];
$cityid[] = $row['cityid'];
$citycount[] = $row['count'];
}


?>
<?
//get_next_start_week() allows me to get the start day for our own weekly newsletter
//Query works fine after much previous testing
$query = "SELECT * FROM events WHERE event_campaign_id='$id' AND date > '".date("Y-m-d H:i:s", get_next_week_start(5))."' ORDER BY date ASC";
$query = mysql_query($query);

if(mysql_num_rows($query) > 0)
{
//Campaign Name  //First Event  //Last Event

?>
<form action=update_promotion.php method=POST id=update_promotion name=update_promotion>
<tr><td><h1>Join Weekly Nightlife Guide (Sent every Wednesday at 5am)</h1></td></tr>
<tr><Td>You can add your event to our event promotion page</td></tr>
<tr><Td height=10px></td></tr>
<tr><td><table width="100%" cellpadding=0 cellspacing=0>
<tr>
<td><b>Event</b></tD>
<td><b>Digital Promotion</b></tD>
</tr>
<?
while($row = mysql_fetch_array($query))
{
$ec_id = $row['ec_id'];
$venueid = $row['venueid'];
$name = $row['event_name'];
$date = date("D dS M Y", get_date_time_stamp($row['date']));

//GET THE VENUE // Very fast query
$venueqry = "SELECT venue_name, cityid FROM venues WHERE venueid='$venueid'";
$venueqry = mysql_query($venueqry);
$vrow = mysql_fetch_array($venueqry);
$venue = $vrow['venue_name'];
$cityid = $vrow['cityid'];

$promoqry = "SELECT * FROM email_promotion WHERE event_id='".$row['eventid']."' AND city_id='$cityid'"; //Again very fast
$promoqry = mysql_query($promoqry);
if(mysql_num_rows($promoqry) > 0)
{
$promorow = mysql_fetch_array($promoqry);
$send_to = $promorow['to_send'];
}
else
{
$send_to = 0;
}

//GET THE CITY FOR THE EVENT
$cityqry = "SELECT city FROM cities WHERE cityid='$cityid'";
$cityqry = mysql_query($cityqry);
$crow = mysql_fetch_array($cityqry);
$city = $crow['city'];


?>
<tr>
<Td style="height:70px;  border-top:1px solid #cccccc;"><? echo $name; ?> [<a href="my_event.php?id=<? echo $row['eventid']; ?>">Edit</a>]<br><? echo $venue; ?> <? if($city != ""){ echo "<br>".$city.""; }?><br><? echo $date; ?></td>
<td style="height:70px;  border-top:1px solid #cccccc;">Email <input type=input id="promo_<? echo $row['eventid']; ?>" name="promo_<? echo $row['eventid']; ?>" value="<? echo $send_to; ?>"></input> members in
<select id="members_<? echo $row['eventid']; ?>" name="members_<? echo $row['eventid']; ?>">
<?

                                        //SUGGESTING AN INFINITE LOOP HERE BUT CANT SEE IT
for($i = 0; $i<count($cities); $i++)
{
echo "<option value='".$cities[$i]."' ";
if($cityid[$i] == $cityid)
{
echo " selected";
}
echo ">".$cities[$i]." (".$citycount[$i].")"."</option>";
}
?>
</select> @ £10 per 1000
<select id="newsletter_<? echo $row['eventid']; ?>">
<?
//Get all the wednesdays nightlife guides that this event can be added to
$start_friday = get_next_week_start(5);
$guide_friday = $start_friday;
$start_wed = get_next_week_start(3);
$guide_date = $start_wed;
$event_stamp = get_date_time_stamp($row['date']) ;
for($j = 0; $event_stamp >= $guide_friday; $j+=7)
{
$guide_date = mktime(0, 0, 0, date("m", $start_wed), date("d", $start_wed) + $j, date("Y", $start_wed));
$guide_friday = mktime(0, 0, 0, date("m", $start_friday), date("d", $start_friday) + $j, date("Y", $start_friday));

echo "<option>".date("D jS M", $guide_date)."</option>";

}
?>

</select>
<br><A href=#>Add More</a></td>
</tr>
<?
}
?>
<tr><td height=10px></td></tr>
<tr>
<td>
<input type=button value="Update Promotion >" onClick ="validatePromotion();">
</td>
</tr>
</table></tr><td>
</form>
<?
}
else
{
//Support number will go in here eventually 0871 number
?>
<tr><td><h1>View Events</h1></td></tr>
<tr><td>You have no events running yet?  Click <a href=new_campaign.php>here</a> to get started</td></tr>
<?
}

 

?>
</table>

As you can see from the above - the code is generally sound but for some reason it is not completing.

Since making the change and moving away from a multidimentional array it is breaking on the first time it runs through the city and not "one of" the last times.

The main query currently returns 19 rows and if i limit it to ten (prior to the change to single dimentional arrays) the script would finish no problem instantly.

I can only think it to be memory.

Paul

Link to comment
Share on other sites

After working a way around this I found that another of our programmers had the same problem and after testing found out that it only happened on our live server and not on any of our test servers.

Does anyone know why php may crash apache because the scripts get longer (They are by no means long) sometimes when doing a mysql_fetch_array 12 times.

I know that you will probably need to know our entire setup but if anyone has any general reasons why this may occur it would be greatly appreciated.

Kind Regards,
themace
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.