phpjay Posted July 9, 2011 Share Posted July 9, 2011 i have a php code not counting the number of rows but when i put my query in sql it will count heres my code what is the problem. <?php // count the records during inserted include('connection.php'); // Connect to server and select databse $connect = mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db")or die("cannot select DB"); $result = mysql_query("SELECT * FROM $table2"); $num_rows = mysql_num_rows($result); $query = mysql_query("SELECT COUNT( * ) FROM tbltracking WHERE TotalOut = 'Out'"); $num = mysql_num_rows($query); $TotalOnHand = ($num_rows - $num); echo "Total In: $num_rows<br>"; echo "Total Out: $num<br>"; echo "Total In Hand: $TotalOnHand"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/241482-query-count/ Share on other sites More sharing options...
gizmola Posted July 9, 2011 Share Posted July 9, 2011 You have some strange code there. Not sure why in one case you select * from $table2, and then in the next query do a SELECT COUNT(*). You should pick one or the other method. Personally, I feel that if you only want a count of the number of rows then SELECT COUNT(*) is more correct. However, if you do a SELECT COUNT(*) you will always have one row in the result set, so you must fetch that row. $query = mysql_query("SELECT COUNT( * ) as countof FROM tbltracking WHERE TotalOut = 'Out'"); $row = mysql_fetch_assoc($query); $num = $row['countof']; Quote Link to comment https://forums.phpfreaks.com/topic/241482-query-count/#findComment-1240443 Share on other sites More sharing options...
phpjay Posted July 9, 2011 Author Share Posted July 9, 2011 heres my code i already solve it: thanks for reply <?php // count the records during inserted include('connection.php'); // Connect to server and select databse $connect = mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db")or die("cannot select DB"); $result = mysql_query("SELECT * FROM $table2"); $num_rows = mysql_num_rows($result); $x = "SELECT COUNT(*) FROM $table2 WHERE TimeOut='Out'"; $result2 = mysql_query($x) or die(mysql_error()); $total_rows = mysql_fetch_row($result2); $toh = ($num_rows - $total_rows[0]); echo "Total In: $num_rows<br>"; echo "Total Out: $total_rows[0]<br>"; echo "Total In Hand: $toh"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/241482-query-count/#findComment-1240447 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.