PHP + MYSQL座位预约系统。计算免费座位

时间:2013-03-13 11:55:41

标签: php mysql for-loop select

我有一张这样的表:

    $sql = "CREATE TABLE dramaStudio
   (
   rowId varchar(1) not null,
   columnId int not null,
   status int,
   firstName varchar(15) not null,
   lastName varchar(15) not null,
   updatedby varchar(10),
   PRIMARY KEY (rowId, columnId)
   )";

在我网站的主页上,我希望用户能够看到是否有任何免费座位而无需进入座位计划。几乎只是说“免费席位:”后跟一个数字。 我在伪代码中的逻辑是:

$j=0 
select * from dramaStudio
for every status == 0
    $j+1
echo $j

非常感谢任何帮助。

4 个答案:

答案 0 :(得分:1)

只需对状态为0的所有字段执行COUNT

SELECT COUNT(*) FROM dramaStudio WHERE status = '0'

答案 1 :(得分:1)

如果空位:

,假设状态= 0
SELECT COUNT(*) AS freeseats FROM dramaStudio WHERE status=0

完整示例,来自php手册:

<?php
  $link = mysqli_connect("localhost", "my_user", "my_password", "world");

  /* check connection */
  if (mysqli_connect_errno()) {
      printf("Connect failed: %s\n", mysqli_connect_error());
      exit();
  }

  $query = "SELECT COUNT(*) AS freeseats FROM dramaStudio WHERE status=0";
  $result = mysqli_query($link, $query);

  /* associative array */
  $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  echo $row['freeseats']; // this is an integer

  /* free result set */
  mysqli_free_result($result);

  /* close connection */
  mysqli_close($link);
?> 

答案 2 :(得分:1)

参见示例代码

$con = mysql_connect("localhost", "peter", "abc123");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

$db_selected = mysql_select_db("test_db",$con);

$sql = "SELECT * from Person";
$result = mysql_query($sql,$con);

while ($property = mysql_fetch_field($result))
  {
  echo "Field name: " . $property->name . "<br />";
  echo "Table name: " . $property->table . "<br />";
  echo "Default value: " . $property->def . "<br />";
  echo "Max length: " . $property->max_length . "<br />";
  echo "Not NULL: " . $property->not_null . "<br />";
  echo "Primary Key: " . $property->primary_key . "<br />";
  echo "Unique Key: " . $property->unique_key . "<br />";
  echo "Mutliple Key: " . $property->multiple_key . "<br />";
  echo "Numeric Field: " . $property->numeric . "<br />";
  echo "BLOB: " . $property->blob . "<br />";
  echo "Field Type: " . $property->type . "<br />";
  echo "Unsigned: " . $property->unsigned . "<br />";
  echo "Zero-filled: " . $property->zerofill . "<br /><br />";
  }

mysql_close($con);

答案 3 :(得分:1)

你正在存放填充的座位?为了找出空,你必须指定colomn和行的数量。

如果填充的空座位可以在表格中区分。 你可以这样做,

    $query = "select * from dramaStudio where status = 0";
    $result_Set = mysql_query($query);
    while($row = mysql_fetch_object($result_set)){
        echo $row->rowId .',', $row->columnId; 
        echo '</br>';
    }