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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

simple validation

Status
Not open for further replies.

cyberspace

Technical User
Aug 19, 2005
968
GB
i'm still in the learning process of php/mysql and i'm trying to add validation etc to pages.

i have this script to show users:

$viewusers = mysql_query("SELECT * FROM users") or
die("Error: Could not select users");


print ("<h2>Current users</h2>");

while ($rec = mysql_fetch_array($viewusers))
{
print ( $rec["username"] . "</p>\n");
}

That works just fine...pretty simple stuff.

What i would also like to do is if the USERS table is empty, have the message "there are no users" instead of showing a blank list.

i tried making an if statement before the initial print statement, eg if users = empty { echo "no records" else etc etc. } but got an error.

What am i doing wrong?

thanks.

'When all else fails.......read the manual'
 
Have a look at

Code:
$viewusers = mysql_query("SELECT * FROM users") or
          die("Error: Could not select users");
$num_rows = mysql_num_rows($viewusers);
if ( $num_rows > 0 ) {          
    
      print ("<h2>Current users</h2>");

      while ($rec = mysql_fetch_array($viewusers))
      {
         print ( $rec["username"] . "</p>\n");
      }
}
else 
{
  print "No record found!!" ;
}

--------------------------------------------------------------------------
I never set a goal because u never know whats going to happen tommorow.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top