Jump to content

query count


phpjay

Recommended Posts

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";



?> 

Link to comment
https://forums.phpfreaks.com/topic/241482-query-count/
Share on other sites

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'];

 

Link to comment
https://forums.phpfreaks.com/topic/241482-query-count/#findComment-1240443
Share on other sites

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";



?> 

Link to comment
https://forums.phpfreaks.com/topic/241482-query-count/#findComment-1240447
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.