views:

26

answers:

5

im new to programming and i need help on my code ? i want my page to prompt me if there will be available rooms left. im using the onload function on the admin page.

so far here is my code

function prompt()
{
< ?php 
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;

can someone help me ? please need this code asap. thank you so much

edit:

the code worked fine now but it displays "Resource id#4"? not the value of the count ? any help with this ? -renz

A: 

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);
Alexander.Plutov
A: 

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.

Sarfraz
sir it displays "Resource id #4" ? not the number of available rooms left ?
renz
@renz: No one is sir here :) Make sure that `$result` is assigned to `mysql_query` function and your query runs successfully.
Sarfraz
thank you so much. i had it working now ! :)
renz
@renz: Welcome...
Sarfraz
oh i think i have a problem on count ? it always displays 1 even i i have lot of rooms left.
renz
@renz: Modify your sql query like this: `SELECT * FROM rooms WHERE status = 'available'`. Now it will return total count :)
Sarfraz
works like a charm ! thank you so much again !
renz
@renz: Welcome again :)
Sarfraz
A: 

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 ?>){
Haim Evgi
+1  A: 

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

xueyumusic
This is correct too. You can mix them, you just have to be conscious of the context that they run in. Throws a lot of new people for a loop.
jocull
yes it true also , thank you very much for the reply. but when i run it, it displays "Resource id #4" ?
renz
A: 
jocull
thank you so much sir ! ive got the code working but it displays "Resource id#4" ?
renz
That's related to what the other people have said about MySQL. Your variable contains a result resource instead the actual records. Since you only have 1 record in this case (the count) you only have to read one row instead of looping the results. I will edit the answer above to show how it should be done. Hope it helps!
jocull