Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what is wrong with my while loop?

Status
Not open for further replies.
Nov 18, 2005
43
US
I just get a blank screen. Connecting to a sql database.

-Dan


<?php

$connect = odbc_connect("dsn","username","password");
if (!$connect)
{exit("Connection Failed: " . $connect);}




$sql = "SELECT * FROM userconfig";
$rs = odbc_exec($connect, $sql);
while($result=odbc_fetch_array($rs))
{
echo "$result[displayname]";
}




?>
 
It doesn{t work becasue this:
Code:
while($result=odbc_fetch_array($rs))

will always be true.

Why, becasue your telling it that while the assignemt of the value can be done to perfomr the loop.
Giving a variable a value even if it is Null will always succeed. you are checking if the assgiment can be done, not wether or not the value is usable.








----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
vacunita
I disagree.
The while loop holds an expression that is evaluated every time it is passed - it is a condition. The whole expression will evaluate to TRUE if the function returns TRUE. If the function returns FALSE, the loop will end.

In this case - following your logic - this would have been an infinite loop. The data would still have been displayed, but, nothing was produced and there was no script timeout error mentioned.

itech20059
The causes could be:
1. The query fails (You don't check for that)
2. There is no data in the table
3. The column name used is misspelled or does not exist

Put an echo inside the loop that shows you if you ever get into it. Also, it is better to use the following notation for associative arrays:
Code:
echo $result['whatever'];
 
Sorry for not replying.

I had fixed it the first day.

I actually did echo $result["whatever"];
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top