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