While loop endlessly

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

While loop endlessly

Post 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
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

Increment $row in the wihle ($row++)
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

..........
let's just say i never posted that :P

ty avatar
8)
User avatar
AVATAr
Forum Regular
Posts: 524
Joined: Tue Jul 16, 2002 4:19 pm
Location: Uruguay -- Montevideo
Contact:

Post by AVATAr »

it happens ;)
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Draco_03
Forum Regular
Posts: 577
Joined: Fri Aug 15, 2003 12:25 pm
Location: Montreal, Canada

Post by Draco_03 »

Ty very much Twig :)
Post Reply