Page 1 of 1

[Solved] Limit in MySQl statement not working

Posted: Thu Dec 22, 2005 7:17 pm
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,

Re: Limit in MySQl statement not working

Posted: Thu Dec 22, 2005 9:36 pm
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.

Posted: Fri Dec 23, 2005 8:22 am
by vincenzobar
That was it!!!!

I commented it out and it worked!!!

you da man!!!!