Forum Moderators: coopster

Message Too Old, No Replies

Stumped - PHP script stops running during foreach loop

PHP Script MySQL

         

dstonefeather

3:44 am on Jul 22, 2010 (gmt 0)

10+ Year Member



Hey,

I am totally stumped on this. I have a php script that is stopping at random times during it's execution. Basically it is displaying some mySQL data. I have tried everything. It works on some queries, but not others, but more than that - it stops at random points during the foreach loop. It is spitting out a table and generally stops after making between 7 -8 records, but not always at the same point. It is very odd. I'm not really a newbie, but this one has me stumped. Could some character in the data be breaking the script? I have looked into that too, there are no similarities in the data between two queries that it breaks on... anyway. Never gives an error (error reporting is turned on full blast) just dies basically at a random spot....Any ideas?

Thanks

the code:



<?php if(isset($_POST['submit'])){ ?>

<br />
<br />

<table id="results_box" cellpadding="0" cellspacing="0">

<?php $bg = 'alt2'; ?>

<?php echo '<pre>',print_r($results['rows']),'</pre>'; ?>

<?php foreach( $results['rows'] as $row){ ?>

<?php $podcasts = getRelatedPodcasts('item',$row['record_id']); ?>
<?php $images = getRelatedImages('item',$row['record_id']); ?>
<?php $main_image = getAndShowMainImage('item',$row['record_id'],'mini'); ?>
<?php $color_class = str_replace(' ','-',$row['category']); ?>
<?php $people = getRelatedPeople('item',$row['record_id']); ?>

<?php $bg = ($bg == 'alt2' ? 'alt1' : 'alt2'); ?>

<tr class="<?php echo $bg; ?>" onClick="Link('index.php?page=entry&permalink=<?=$row['record_id']; ?>')">
<td class="leftrows <?=$color_class?>">
<?=$main_image?>
</td>
<td class="next-to-leftrows " width="25%">
<div class="text-headroom">
<font class="title-medium"><?php echo highlight($row['name_title'],$_POST['search_param']); ?></font><br />
<span class="small"><?=highlight($row['city'],$_POST['search_param'])?>, <?=$row['state']?> &bull; <?=highlight($row['category'],$_POST['search_param'])?></span>
</div>
</td>
<td class="rows" width="25%">
<div class="text-headroom">
<ul class="small">
<?php
foreach($people['record_ids'] as $key => $person){ ?>
<li><?=highlight(getPersonName($person,'FL'),$_POST['search_param'])?></li>
<?php } ?>
</ul>
</div>
</td>
<td class="rows small" width="45%"><?php // echo constrainLongText($row['remarks'],150); ?></td>
<td class="rightrows small" align="right" width="25">
<?php if($podcasts['count'] > 0){ ?>
<img src="ui/images/headphones.png" />
<?php } ?>
</td>
</tr>

<?php }

} // end if submit for results ?>

</table>

<?php if(isset($_POST['submit'])){ ?>

<div class="fullwidth">
<div class="floatleft leftalign half">
<br />
Your search returned <?=$results['count']?> results<?php if($results['count'] >= 2 ? 's' : ''); ?>.
<br /><br />
</div>
<div class="floatright rightalign half">
<br />
<?php
echo prevNextRecordsGroup(currentPageURL(),stickyForm('start_at','0'),stickyForm('rows_to_show','25'),$results['count']);
?>
<br /><br />
</div>

<?php } // end if submit for paging ?>

</div><!-- /bluebox -->



And my search function:



function basicItemSearch($search_param = '',$start_at = 0,$rows_to_show = 25){

if($rows_to_show == 'all'){
$rows_to_show = '';
$limit = '';
}else{
$limit = ' LIMIT ' . $start_at . ', ' . $rows_to_show;
}


$q = "SELECT DISTINCT item.*
FROM item, item_people, people
WHERE item.record_id = item_people.item_id AND people.record_id = item_people.people_id
AND
(concat_ws(' ',people.name_first,people.name) LIKE '%$search_param%' OR
item.name_title LIKE '%$search_param%' OR
item.city = '$search_param' OR
item.category LIKE '%$search_param%' OR
item.on_lists LIKE '%$search_param%')
$limit";

//echo 'query: ',$q,'<hr/>';

$r = @mysql_query($q) OR die('unable to execute query <i>' . $q . '</i>: ' . mysql_error());

while($row = mysql_fetch_array($r, MYSQL_ASSOC)){
$row = clean_array($row);
$rows[] = $row;
}

$results = array('query' => $q,'count' => '55', 'rows' => $rows);

return $results;

}



Thank you.

dstonefeather

3:47 am on Jul 22, 2010 (gmt 0)

10+ Year Member



Oh yeah, you can see where I print out the array just to take a look, so it is coming out of the database correctly, it displays there in full. It is only in making the display that it breaks.

mack

5:02 am on Jul 22, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



your php.ini might be limiting execution time?

Mack.

Matthew1980

7:42 am on Jul 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there dstonefeather,

may be inconsequential but if the ini file in php doesn't support short tags, ie it's set to FULL tags then inside the loop will fail:

<?php
foreach($people['record_ids'] as $key => $person){ ?>
<li><?php=highlight(getPersonName($person,'FL'),$_POST['search_param']);?></li>

that and your missing the closing ; from that statement :)

I personally have never understood the point of prefixing '@' to a function to suppress error's surely you want to know about any errors?

Hope that helps.

Cheers,
MRb

rocknbil

6:25 pm on Jul 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Might not be helpful, but is it both loops or just the search one?

Be cautious with @:

@mysql_query

The @ operator, when prepending any function, suppresses error messages but does not suppress return values.

Error control operators [php.net]

However, you have "or die" and that does effectively capture a problem executing the query.

As for the problem . . . what do your server error logs say, anything? That's the first stop I would think . . . I am guessing timeout or memory limit (but that is **just a guess**).

I'd begin debugging by, one by one, commenting out the various function calls, get it down to the bare bones just to get it to run.

dstonefeather

7:04 am on Jul 26, 2010 (gmt 0)

10+ Year Member



Great, thanks for the insight. Sorry it took so long to respond, I thought I had this set to email me when replies cam but I didn't. Here I thought I was alone in the world.

thanks!

I'm still not sure. I don't think it is a timeout, I mean, this returns other much longer results no problem. Plus, that would send an error message.

I got rid of the @, nothing there. Funny, I must have pulled that from somewhere years ago and never even bothered to realize what the hell it was doing there.

I changed all the tags to full. Still getting the smae behavior.

I'm on a mission. Will post the result for sure. Thanks for the help.

Oh, and I can't see my error logs because my host:

"Since Aplus.Net provides a production environment, as opposed to a development environment, we do not allow customers access to the error logs. You should develop your site (CGI, HTML, etc.) on your local computer, and then upload it when everything is completed. Developing/testing your site directly on Aplus.Net’s servers might cause a leakage in the incomplete scripts, which leads to high CPU load on Aplus.Net’s servers, which is against Aplus.Net’s policy."

Alcoholico

10:55 am on Jul 26, 2010 (gmt 0)

10+ Year Member



What php and MySQL versions are you using? A few months ago I had a similar problem, I can't remember the details but it turned out it was not php, the problem had to do with the MySQL version I was using on the windows testing server, I wasted a lot of time trying different solutions I found, no luck. Once the whole thing was moved to the Linux production server never knew again of it. I'm not saying that's what's happening to your script, but could be.