I'm new to programming and I need help on my code. I want my page to prompt me if there will be available rooms left. I'm using the onload function on the admin page.
so far here is my code
function prompt()
{
< ?php
开发者_JS百科include("dbconfig.php");
$sql = "SELECT COUNT(*) FROM rooms WHERE status = 'available'";
$result = @mysql_query($sql) or die("Could not execute query");
?>
if(< ?php $result <= 14 ?>){
alert("Rooms left: < ?php echo $result ?>");
}
else{
alert("Welcome Admin.");
}
}
window.onload=prompt;
edit:
The code worked fine now but it displays "Resource id#4", not the value of the count.
I feel you can't mix php with js codes. php is mainly on server side , while the js is client side from the snippet you provide, maybe you should use purely php as follows:
< ?php
include("dbconfig.php");
$sql = "SELECT COUNT(*) FROM rooms WHERE status = 'available'";
$result = @mysql_query($sql) or die("Could not execute query");
if ($result <= 14) {
echo("Rooms left: $result");
}
else {
echo("Welcome Admin.")
}
?>
This should be run at the first when request
I think you are confused about where PHP processes vs. where Javascript processes.
PHP is processed on the server side, while Javascript is processed on the client side. Think of it like this...
- You access a page.
- Your PHP is processed, and the final output is sent to the browser.
- Your Javascript is processed by the browser.
As you have it now, you'd be getting some funny output... especially because of your lack of echo statements. Here is what you'd probably be seeing in your browser page source:
function prompt()
{
if(){
alert("Rooms left: < ?php echo $result ?>");
}
else{
alert("Welcome Admin.");
}
}
window.onload=prompt;
Notice the empty if statement (also the space in the start tags:
if(<?php echo ($result <= 14); ?>){
alert("Rooms left: <?php echo $result ?>");
}
This should make your Javascript evaluate a boolean true/false. Don't forget that Javascript needs to be wrapped in a < script > tag too!
To answer your MySQL question... Try it like this:
//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.
mysql_query
returns resource, not a result.
Try to use:
$sql = "SELECT COUNT(*) FROM `rooms` WHERE `status` = 'available'";
$res = @mysql_query($sql) or die('Could not execute query');
$count = mysql_result($res, 0, 0);
There should be no space in php tags:
< ?php
^
Should be:
<?php
You are also missing a fetching function, here is how you can get row count in a variable:
<?php $count = mysql_num_rows($result);?>
Later you can use the $count
variable in the if
condition.
use mysql_fetch_row , and after that in the condition , compare to $row[0]
$sql = "SELECT COUNT(*) FROM `rooms` WHERE `status` = 'available'";
$res = @mysql_query($sql) or die('Could not execute query');
$row = mysql_fetch_row($res);
if(< ?php $row[0] <= 14 ?>){
精彩评论