1

我有一张这样的桌子:

    $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

4 回答 4

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);
于 2013-03-13T12:04:50.203 回答
1

只需对所有状态为 0 的字段进行计数?

SELECT COUNT(*) FROM dramaStudio WHERE status = '0'
于 2013-03-13T11:57:55.517 回答
1

假设如果有空闲座位,则 status=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);
?> 
于 2013-03-13T11:58:32.110 回答
1

您要存放已坐满的座位吗?要找出空的,您必须指定列数和行数。

以及是否可以根据表格中的状态区分已满座和空座。你可以这样做,

    $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>';
    }
于 2013-03-13T12:06:27.700 回答