Page 1 of 1

While loop endlessly

Posted: Mon Dec 22, 2003 1:11 pm
by Draco_03
mhhh i dunno why but my while just doesn't doesnt stop...

Code: Select all

$host = "you";
$user = "guess";
$pswd = "what";

mysql_select_db ("spanking_is_good");
$connect = mysql_connect($host, $user, $pswd) 
	or die("Could not connect: " . mysql_error());
$database = mysql_select_db('spanking_is_good') 
	or die(MySQL_Error());
$sql = "SELECT * FROM catalogue";
$result = mysql_query($sql) or die(MySQL_Error());
$row=0;
while($row <= mysql_num_rows($result)){
	$get = mysql_fetch_row($result);
	print "$get[0] : $get[1]<br />";
	}
mysql_close($connect);
That output something like

Code: Select all

1 : www.i_like_to_be_spanked.com
:
:
:
etc

Posted: Mon Dec 22, 2003 1:14 pm
by AVATAr
Increment $row in the wihle ($row++)

Posted: Mon Dec 22, 2003 1:16 pm
by Draco_03
..........
let's just say i never posted that :P

ty avatar
8)

Posted: Mon Dec 22, 2003 1:34 pm
by AVATAr
it happens ;)

Posted: Tue Dec 23, 2003 3:04 am
by twigletmac
You could simplify this:

Code: Select all

$row = 0;
while($row <= mysql_num_rows($result)){
   $get = mysql_fetch_row($result);
   print "$get[0] : $get[1]<br />";
   $row++;
   }
to

Code: Select all

while ($row = mysql_fetch_row($result)) {
	print $row[0].' : '.$row[1];
}
BTW, if you used [php_man]mysql_fetch_assoc[/php_man]() you could use the column names instead of having to remember in which order they are in the table. Displaying according to order in the table could cause a lot of problems if the table structure is changed.

Mac

Posted: Tue Dec 23, 2003 9:20 am
by Draco_03
Ty very much Twig :)