Jump to content

[SOLVED] query loop o DOOM


ccrevcypsys

Recommended Posts

hello i have a query that loops about 1000000000x1010101010101010109182309 times and i dont undersatand why it would be doing this. here is my code

<?php 
	if($editOpts[0]['id']=="1"){
	$type = "courses";
	}elseif($editOpts[0]['id']=="2"){
	$type = "diary";
	}elseif($editOpts[0]['id']=="3"){
	$type = "events";
	}elseif($editOpts[0]['id']=="4"){
	$type = "newsletter";
	}elseif($editOpts[0]['id']=="5"){
	$type = "office_address_list";
	}
		$query="SELECT * FROM ".$type;
$mainQuery = $db->select($query, $PerPage, $page);
$totalNoProducts = $db->numrows($query);

	?>
        
        <span class="oldOpts"><h2>Edit <?php echo $editOpts[0]['name'] ?></h2>
            <?php 
		if($editOpts == TRUE){ ?>
        <table width="100%" border="0" cellpadding="1">
		<tr>
            	
            	<td>Event Key</td>
                <td>First Name</td>
                <td>Last Name</td>
                <td>Event Name</td>
                <td>Description</td>
                <td>Event Date</td>
                <td>Event Hours</td>
                <td>Reg No.</td>
                <td>Sub Head</td>
                <td>Option</td>
		</tr>
	<?php for ($i=0; $i<$mainQuery;$i++) { ?>
      <tr>
            	<td><?php echo $mainQuery[$i]['EventKey']; ?></td>
            	<td><?php echo $mainQuery[$i]['FirstName']; ?></td>
            	<td><?php echo $mainQuery[$i]['LastName']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventName']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventDescription']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventDate']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventHours']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventKey']; ?></td>
            	<td><?php echo $mainQuery[$i]['RegNo']; ?></td>
            	<td><?php echo $mainQuery[$i]['SubHead']; ?></td>
            </tr>
            <?php  } ?>
</table>
                
<?php } ?>

Link to comment
Share on other sites

i took a second look at your code, and it's a little confusing. it looks like you've taken the longer more difficult route for most of it. and it also seems like something's missing. are you using a pagination script to display your queried results? that would be important to know.

 

there are definitey better ways of doing this, but if you want to keep your code and make it work i think this is all you need to do:

<?php for ($i = 0; $i < count($mainQuery); $i++) { ?>
      <tr>
            	<td><?php echo $mainQuery[$i]['EventKey']; ?></td>
            	<td><?php echo $mainQuery[$i]['FirstName']; ?></td>
            	<td><?php echo $mainQuery[$i]['LastName']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventName']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventDescription']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventDate']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventHours']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventKey']; ?></td>
            	<td><?php echo $mainQuery[$i]['RegNo']; ?></td>
            	<td><?php echo $mainQuery[$i]['SubHead']; ?></td>
            </tr>
            <?php  } ?>

 

notice how i added the count() function to the for loop parameters. $i has to be compared to an integer, since it is an integer itself. but you were trying to compare it to an array. now, with the count function, it will be compared to the number of elements in the array, that should work. as long as i'm understanding what you're trying to do, then it should work.

Link to comment
Share on other sites

well theres 1181 querys and it goes all the way up until that when i did the

print_r($mainQuery);

 

so it doesnt explain y it would do that a billion times... any other suggestions?

 

i took a second look at your code, and it's a little confusing. it looks like you've taken the longer more difficult route for most of it. and it also seems like something's missing. are you using a pagination script to display your queried results? that would be important to know.

no i took that out for now that shouldnt be in there... lol

Link to comment
Share on other sites

well that doesnt work.. it just says array

 

Well you just answered your own question then.  If $mainQuery is an array and you are using this loop:

for ($i=0; $i<$mainQuery;$i++) {

then you are comparing a number to an array.

 

could it be because i am using xampp or what?

When debugging, it helps to be methodical and observant; jumping to wild conclusions rarely helps.

Link to comment
Share on other sites

should this not be counting the number of rows returned and not the main query

<?php for ($i = 0; $i < $totalNoProducts; $i++) { ?>
      <tr>
            	<td><?php echo $mainQuery[$i]['EventKey']; ?></td>
            	<td><?php echo $mainQuery[$i]['FirstName']; ?></td>
            	<td><?php echo $mainQuery[$i]['LastName']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventName']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventDescription']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventDate']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventHours']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventKey']; ?></td>
            	<td><?php echo $mainQuery[$i]['RegNo']; ?></td>
            	<td><?php echo $mainQuery[$i]['SubHead']; ?></td>
            </tr>
            <?php  } ?>

Link to comment
Share on other sites

ccrevcypsys, did you try the code i gave you? all you gotta do is change that one line in the for loop. let me know if that works.

 

paul, i think those are two different queries. it's hard to tell but i don't think they're related.

 

roopurt, you just repeated what i said before.

Link to comment
Share on other sites

i was just wondering if there where any bugs im not aware of. And it did this on one of my other pages 2 not just this page it is happening every time i try to query. So i can assume it might be that its not just some wild conclusion...

You will go much further when programming if you exhaustively test your own code and logic before assuming there is a bug in the software you are using.

 

Think about this logically.  Selecting data from a database and displaying it via a loop is probably one of the most common functions of any PHP script.  The chances of you blindly stumbling across a bug in PHP, Apache, or MySQL while performing this operation are slim-to-none when you consider the millions of PHP scripts in existence that already perform this function.

 

Mind you I'm not trying to deride you or make you feel bad; I'm merely explaining why:

could it be because i am using xampp or what?

is a wild conclusion.

 

When debugging your scripts, stick with what you know.  I'll walk you through it one step at a time.

 

1) You know that your script is executing and looping, thus we can assume there are no syntax errors and that everything is likely correct up until the looping portion.  So we examine the loop:

<?php for ($i=0; $i<$mainQuery;$i++) { ?>
      <tr>
            	<td><?php echo $mainQuery[$i]['EventKey']; ?></td>
            	<td><?php echo $mainQuery[$i]['FirstName']; ?></td>
            	<td><?php echo $mainQuery[$i]['LastName']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventName']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventDescription']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventDate']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventHours']; ?></td>
            	<td><?php echo $mainQuery[$i]['EventKey']; ?></td>
            	<td><?php echo $mainQuery[$i]['RegNo']; ?></td>
            	<td><?php echo $mainQuery[$i]['SubHead']; ?></td>
            </tr>
            <?php  } ?>

 

2) The body of the loop only echoes data, so we can assume it is irrelevant to the fact that the loop is looping forever.  So let's just imagine the body isn't there.

<?php for ($i=0; $i<$mainQuery;$i++) { ?>
      
            <?php  } ?>

 

3) Well, it certainly looks like the loop is set up correctly.  We start at zero and iterate until $i is less than $mainQuery.  But we are looping forever, let's print $mainQuery to see if it is what we expected it was:

<?php echo '<pre style="text-align: left;">' . print_r($mainQuery, true) . '</pre>';
<?php for ($i=0; $i<$mainQuery;$i++) { ?>
            <?php  } ?>

 

4) At that point you would have seen the contents of your array dumped to the screen and a lightbulb should go off in your head.  You can't compare an int to an array and expect a meaningful result.  This means that you must either change the upper constraint on your loop (so that it is an integer) or change your loop from a for to a foreach.

 

:D

 

 

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.