[Solved] Limit in MySQl statement not working

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

[Solved] Limit in MySQl statement not working

Post by vincenzobar »

ok here is the code

Code: Select all

$order='ASC';
$numRows =5;
if ($_SERVER['HTTP_CONNECTION']){
		$limit = $numRows ;
		$query_id_rs = "SELECT id FROM textads WHERE visible='y' AND active='y' ORDER BY date_created ".$order." LIMIT ".$limit."";
		$id_rs = mysql_query($query_id_rs, $db_connect) or die(mysql_error());
		$all_visible_ids = mysql_fetch_assoc($id_rs);
		
				while($all_visible_ids = mysql_fetch_assoc($id_rs)){
					$sql_query = 'UPDATE textads SET view_count = view_count+1 WHERE id="'.$all_visible_ids['id'].'"';
					mysql_query($sql_query, $db_connect) or die(mysql_error());
					// for testing
					//print_r($all_visible_ids);
									
				}
		}
It seems to be dropping the first record and only increment to four records. I tried $limit = $numRows +1; and all that did was skip the first record in the fetch and add to 5 preceeding records.

WTF anyone know whats up with this little bug? How to work around it?

thanks,
User avatar
neophyte
DevNet Resident
Posts: 1537
Joined: Tue Jan 20, 2004 4:58 pm
Location: Minnesota

Re: Limit in MySQl statement not working

Post by neophyte »

Code: Select all

$all_visible_ids = mysql_fetch_assoc($id_rs); //This line calls your first row and puts it in $all_visible_ids
		
				while($all_visible_ids = mysql_fetch_assoc($id_rs)){ //This gets 2-5 rows but over writes row 1
					$sql_query = 'UPDATE textads SET view_count = view_count+1 WHERE id="'.$all_visible_ids['id'].'"';
					mysql_query($sql_query, $db_connect) or die(mysql_error());
					// for testing
					//print_r($all_visible_ids);
									
				}
		}
Dunno what your trying to accomplish but the first row is being wiped out in the first line of code above. See my comments in the code.
vincenzobar
Forum Commoner
Posts: 95
Joined: Wed Nov 02, 2005 9:57 am

Post by vincenzobar »

That was it!!!!

I commented it out and it worked!!!

you da man!!!!
Post Reply