mark103 Posted June 11, 2011 Share Posted June 11, 2011 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 https://forums.phpfreaks.com/topic/239092-trying-to-count-the-database-rows/ Share on other sites More sharing options...
fugix Posted June 11, 2011 Share Posted June 11, 2011 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 https://forums.phpfreaks.com/topic/239092-trying-to-count-the-database-rows/#findComment-1228431 Share on other sites More sharing options...
mark103 Posted June 11, 2011 Author Share Posted June 11, 2011 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 https://forums.phpfreaks.com/topic/239092-trying-to-count-the-database-rows/#findComment-1228436 Share on other sites More sharing options...
fugix Posted June 11, 2011 Share Posted June 11, 2011 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 https://forums.phpfreaks.com/topic/239092-trying-to-count-the-database-rows/#findComment-1228443 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.