Jump to content

trying to count the database rows


mark103

Recommended Posts

Hi guys,

 

I have a bit of a problem with print out the rows from mysql database. I am trying to count how many rows there are in a table, but I get this: There are 2 Users Online right nowThere are 2 Users Online right nowError: Query was empty

 

I want to print out something like this: There are 2 Users Online right now

 

<?php
session_start();
    define('DB_HOST', 'localhost');
    define('DB_USER', 'dbusername');
    define('DB_PASSWORD', 'dbpass');
    define('DB_DATABASE', 'dbname');
       
    $errmsg_arr = array();
    $errflag = false;

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
  die('Failed to connect to server: ' . mysql_error());
    }

    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {

die("Unable to select database");
    }

   function clean($var){

return mysql_real_escape_string(strip_tags($var));
    }
    $username = clean($_GET['user']);


if($username == '' && $pass == ''){
   // both are empty
   $errmsg_arr[] = 'Username are missing.';
   $errflag = true;
}

    if($errflag) {
  $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
  echo implode('<br />',$errmsg_arr);
   }
   else {

$insert = array();
if(isset($_GET['user'])) {
    $insert[] = 'username = \'' . clean($_GET['user']) .'\'';
}


if (count($insert)>0) {
   $names = implode(',',$insert);


if($username == 'all') {
  $names = implode(',',$insert);
  $result = mysql_query("SELECT * FROM Online_Users");
  $num_rows = mysql_num_rows($result);

while ($row = mysql_fetch_array($result)) {
  echo "There are $num_rows Users Online right now";
  }

if (!mysql_query($sql,$link))
  {
  die('Error: ' . mysql_error());
  }
}
}
?>

 

 

Any advice would be much appreicate.

 

Thanks,

Mark

Link to comment
Share on other sites

there is no reason to use a while loop here, also, in your if statement to check if your query was successful, you are using the wrong variable. $sql should be $result, so of course you will receive a "query empty" error, change your code accordingly

<?php
session_start();
    define('DB_HOST', 'localhost');
    define('DB_USER', 'dbusername');
    define('DB_PASSWORD', 'dbpass');
    define('DB_DATABASE', 'dbname');
       
    $errmsg_arr = array();
    $errflag = false;

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
  die('Failed to connect to server: ' . mysql_error());
    }

    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {

die("Unable to select database");
    }

   function clean($var){

return mysql_real_escape_string(strip_tags($var));
    }
    $username = clean($_GET['user']);


if($username == '' && $pass == ''){
   // both are empty
   $errmsg_arr[] = 'Username are missing.';
   $errflag = true;
}

    if($errflag) {
  $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
  echo implode('<br />',$errmsg_arr);
   }
   else {

$insert = array();
if(isset($_GET['user'])) {
    $insert[] = 'username = \'' . clean($_GET['user']) .'\'';
}


if (count($insert)>0) {
   $names = implode(',',$insert);


if($username == 'all') {
  $names = implode(',',$insert);
  $result = mysql_query("SELECT * FROM Online_Users");
  $num_rows = mysql_num_rows($result);
  echo "There are $num_rows Users Online right now";

if (!mysql_query($result,$link))
  {
  die('Error: ' . mysql_error());
  }
}
}
?>

Link to comment
Share on other sites

thanks for your quick responded. i have replaced the code you have posted, but now I am getting this: There are 2 Users Online right nowError: Query was empty. i want to remove or even hide the Error: Query was empty, but i can't find where the problem is occuring.

 

any idea?

Link to comment
Share on other sites

okay lets revise this again to pinpoint what is happening.

<?php
session_start();
    define('DB_HOST', 'localhost');
    define('DB_USER', 'dbusername');
    define('DB_PASSWORD', 'dbpass');
    define('DB_DATABASE', 'dbname');
       
    $errmsg_arr = array();
    $errflag = false;

    $link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);
    if(!$link) {
  die('Failed to connect to server: ' . mysql_error());
    }

    $db = mysql_select_db(DB_DATABASE);
    if(!$db) {

die("Unable to select database");
    }

   function clean($var){

return mysql_real_escape_string(strip_tags($var));
    }
    $username = clean($_GET['user']);


if($username == '' && $pass == ''){
   // both are empty
   $errmsg_arr[] = 'Username are missing.';
   $errflag = true;
}

    if($errflag) {
  $_SESSION['ERRMSG_ARR'] = $errmsg_arr;
  echo implode('<br />',$errmsg_arr);
   }
   else {

$insert = array();
if(isset($_GET['user'])) {
    $insert[] = 'username = \'' . clean($_GET['user']) .'\'';
}


if (count($insert)>0) {
   $names = implode(',',$insert);


if($username == 'all') {
  $names = implode(',',$insert);
  $result = mysql_query("SELECT * FROM Online_Users") or die('Error: ' . mysql_error());
  $num_rows = mysql_num_rows($result);
  echo "There are $num_rows Users Online right now";

}
}
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.