PHP Results Not Showing

RedWolf

Golden Member
Oct 27, 1999
1,064
0
76
I'm getting the number of rows but I can't get the username (or any other field) to write to the page. No errors are being thrown.


$db=mysql_connect("server", "user", "password");
mysql_select_db("resnet",$db);
$result= mysql_query("select * from wireless",$db);
$num_results= mysql_num_rows($result);
echo $num_results;
while ($myrow = mysql_fetch_row($result));
{
echo $myrow["username"];
}
 

Drakkon

Diamond Member
Aug 14, 2001
8,401
1
0
do a print_r on $myrow and see what comes out. you may be addressing the "username" array var incorrectly or it may be all in caps or somethign wierd.
 

Modeps

Lifer
Oct 24, 2000
17,254
44
91
Your while statement is pretty screwy looking. In my opinion you shouldnt be setting a variable in a while condition. Plus, I believe mysql_fetch_row will only give you one row and your query is definately going to return many more.... so even if the while statement was working, it would probably turn into an infinate loop

do something like this instead:
 

Modeps

Lifer
Oct 24, 2000
17,254
44
91
Everytime the mysql_fetch_array($result) is called, it will keep incrementing which result it returns? I never knew that. But my code is still shorter and less dangerous ;). I tend to avoid while loops just because they're pretty easy to screw up.
 

RedWolf

Golden Member
Oct 27, 1999
1,064
0
76
Got it to work with both of those and figured out what was wrong with mine.

while ($myrow = mysql_fetch_row($result));

should be

while ($myrow = mysql_fetch_row($result))

Now, I'm trying to get it to pull ONLY the last row. However, when I try to change the sql query to:

select top 1 ID, IPAddress from wireless order by ID desc

I get the following error:

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in W:\html\secure\resnet\wireless\dhcp.php on line 10

It would be easy enough to change the loop but shouldn't this work? I generally work with ASP and things seem to translate fairly well but not everything. Also, does anyone have any suggestions for a web site that would aid in transitioning to PHP from ASP?

Thanks for all the help.