0

the function keeps returning 1. if i echo $i within the function then it counts up to six. the function should return 6, because there are 6 results from the mysql_query, so $i iterates upto 6. please any help solving this?

$i=0;

$thewidth = $database->width($theid, $i);

echo $thewidth;
function width($theid,$i)
{
$get =  mysql_query("SELECT * FROM block WHERE sid='$theid'",$this->connect);
			
$i++;		
						
while($row = mysql_fetch_assoc($get)) {
							
						
	$number = $row['id'];
							
	$this->width($number, $i);
							
	return $i;	 
					
		}  
											
				
}
3
Contributors
4
Replies
5
Views
6 Years
Discussion Span
Last Post by kekkaishi
0

if u want to count the number of rows a mysql result set carries, you could use mysql_num_rows function.

$sql = "SELECT * FROM table WHERE id={$id}";
$number_of_rows = mysql_num_rows($sql);
0

It will always return 1 as you're not incrementing $i within your loop - at the minute you're passing the function the number 0 and then adding 1 to it - your logic is flawed. You need to change your function:

function width($theid,$i) {
$get =  mysql_query("SELECT * FROM block WHERE sid='$theid'",$this->connect);
							
while($row=mysql_fetch_assoc($get)){										
	$number = $row['id'];					
	$this->width($number, $i);
	$i++;	 
					
}  

return $i;	
			
}

Alternatively, you could just return a row count as mentioned above.

I'd highly recommend you have a look at the basic tutorials on lopps on w3schools.com.

0

the variable in the WHERE clause is the result of that WHERE clause. if that sounds confusing. basically the next result in the query is dependent on the previous result. If i used a while loop then i would have to keep on repeating the while loop for every result. With function recursion it repeats it for me.

unless you know a better way than function recursion to loop the below so the result($number) of the query is inputted back into the WHERE clause and repeated.

$get =  mysql_query("SELECT * FROM block WHERE sid='$number'",$this->connect);
								
while($row = mysql_fetch_assoc($get)) {
							
						
	$number = $row['id'];
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.