我认为您对 PHP 处理位置与 Javascript 处理位置感到困惑。
PHP 在服务器端处理,而 Javascript 在客户端处理。像这样想...
- 您访问一个页面。
- 处理您的 PHP,并将最终输出发送到浏览器。
- 您的 Javascript 由浏览器处理。
正如你现在所拥有的那样,你会得到一些有趣的输出......尤其是因为你缺少echo语句。以下是您可能会在浏览器页面源代码中看到的内容:
function prompt()
{
if(){
alert("Rooms left: < ?php echo $result ?>");
}
else{
alert("Welcome Admin.");
}
}
window.onload=prompt;
注意空的if 语句(也是开始标签中的空格:
if(<?php echo ($result <= 14); ?>){
alert("Rooms left: <?php echo $result ?>");
}
这应该使您的 Javascript 评估布尔值 true/false。不要忘记 Javascript 也需要包含在 <script> 标记中!
要回答您的 MySQL 问题...
试试这样:
//We can alias the COUNT(*) as MyCount for easy reference
$sql = "SELECT COUNT(*) as MyCount FROM rooms WHERE status = 'available'";
$result = @mysql_query($sql) or die("Could not execute query");
$row = mysql_fetch_array($result); //$row is now an array and will now have your count in it
echo $row['MyCount']; //This will print the count from the database. You could use it in other ways as well.