我正在尝试进行座位预订系统,如果座位X可用,我需要通过MySQL查询。如果指定了一个名称,请打印"抱歉,此座位已被占用",否则如果没有分配名称(默认为0),则打印"可用!"
这是我试过的:
<?php
$link = mysql_connect(localhost, root, root);
mysql_select_db(room);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT name FROM seats WHERE seat = 'A1'");
if ($result == '0') {
echo 'available!';
} else {
echo 'Sorry, this seat is already taken.';
}
}
?>
答案 0 :(得分:1)
您应该考虑远离mysql_ *函数。无论哪种方式,你应该做这样的事情:
http://php.net/manual/en/function.mysql-num-rows.php
$result = mysql_query("SELECT name FROM seats WHERE seat = 'A1'");
if (mysql_num_rows($results) == 0) {
echo 'available!';
} else {
echo 'Sorry, this seat is already taken.';
}
你最后还有一个额外的}
。
答案 1 :(得分:0)
试试这个:
$result = mysql_query("SELECT name FROM seats WHERE seat = 'A1'");
if (mysql_num_rows($result)==0) {
echo 'available!';
}else {
echo 'Sorry, this seat is already taken.';
}
答案 2 :(得分:0)
试试这个:
<?php
$link = new mysqli("localhost", "root", "root", "room");
if ($link->connect_errno)
{
die('Could not connect: ' . $mysqli->connect_error);
}
if (($result = $link->query("SELECT name FROM seats WHERE seat = 'A1'") && $result->num_rows)
{
echo 'Sorry, this seat is already taken.';
}
else
{
$result->close();
echo 'available!';
}
这是使用新的MySQLi library,并应正确检查是否返回任何行的结果。可能没有必要检查$result->num_rows
,因为如果没有返回任何行,$result
应为空。