...

View Full Version : Need help with PHP code



bennystromberg
12-29-2003, 08:16 PM
Whatīs wrong with this code? I donīt even get an error message, just a blank page. Thanks a lot for your help!



include ('connectionfile.php');
$sql_select = ("SELECT var1, var2 from table WHERE username = '".$_SESSION['username']."'");
$result = mysql_query($sql_select);

if ($result){
if (mysql_num_rows($result)== 0){
echo ("<br /> No records returned.");
}
else{

echo("<table><tr><td>Var1</td><td>var2</td></tr>");

while ($row = mysql_fetch_array($result)) {
echo("<tr><td>" . $row['var1'] . "</td><td>" . $row['var2'] . "</td></tr>");
}
echo ("</table>");
}
}

V@no.
12-29-2003, 09:29 PM
maybe something wrong with 'connectionfile.php' file?

me'
12-29-2003, 09:58 PM
Do you have an errors log assigned? If so, look in there. If not, assign one.

bcarl314
12-29-2003, 11:14 PM
And a friendly tip,

when debuggin, add lots of print statements. It seems to be the easiest way to figure out why code is mis behaving.

In this case, you have


if($result) {
// do lots
}

but you don't have an "else { print "no results";}"
which may help you identify the problem as being a bad query.

So, you could do a "print $sql_select;" statment and try to execute that on the db to see if you actually get the data your expecting.

I always like to add print statments then comment them out / delete them when I'm done.
Just a tip

Celtboy
12-30-2003, 02:01 PM
also possible that your sql query isn't returning anything.

Try this:


include ('connectionfile.php');
$sql_select = ("SELECT var1, var2 FROM table WHERE username = '".$_SESSION['username']."'");
$result = mysql_query($sql_select);

if ($result) {
if (mysql_num_rows($result)== 0) {
echo ("<br /> No records returned.");
} else {
echo("<table><tr><td>Var1</td><td>var2</td></tr>");

while ($row = mysql_fetch_assoc($result)) {
echo("<tr><td>" . $row['var1'] . "</td><td>" . $row['var2'] . "</td></tr>");
}
echo ("</table>");
}
} else { print "Whoops...nothing returned from SQL Statement!";}


I'd also suggest viewing the HTML source of that "blank" page. Make sure it isn't really blank....it may be writing that table information to it....and not even telling you. ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum