0

Currently I have two tables A and B. The DID is input by the user into into a input, and that is the first prompt in my program, to enter the DID.

A contains PID, DID, and MaxOccupancy. PID is professional id, DID is department id. B contains CID, DID, and PID. CID is client ID, and DID and PID is same.

I'm outputting data from the first table into an html table which shows all that data.

However, I'm having trouble running a count on number of PID from table two. I have $countB= mysql_query ("select count (PID) from B where DID=('$_POST[DID])");

I am then writing

while($row = mysql_fetch_array($countB))
  {
  echo "Current occupancy:".$row['count(PID)'].;
  }

Can someone help me? How do i do a where in my query for the count to get what the user put in for DID in the input box? Am i doing it wrong completely?

THANkS!

user1354934
  • 8,139
  • 15
  • 50
  • 80
  • `where DID=('$_POST[DID])` contains a stray single quote. Also, look into [mysql_real_escape_string](http://php.net/manual/en/function.mysql-real-escape-string.php). – Herbert Apr 25 '12 at 00:21

2 Answers2

1

Try

$countB= mysql_query (sprintf("SELECT COUNT(PID) as total FROM B WHERE DID=('%s')",mysql_real_escape_string($_POST['DID'])));
$row = mysql_fetch_array($countB);
echo $row['total'] ;

Note .. always filter for SQL Injection

Baba
  • 94,024
  • 28
  • 166
  • 217
0

You can create an alias for the result of the COUNT which you can access in PHP:

mysql_query ("select count (PID) as PIDcount from B where DID=('$_POST[DID])");
// then reference it later
$row['PIDcount'];

The usage of $_POST[DID] in your query is subject to SQL Injection as well.

Community
  • 1
  • 1
drew010
  • 68,777
  • 11
  • 134
  • 162