Jump to content

compare mysql row value to php variable


kaiman

Recommended Posts

Hi again everyone,

 

I am trying to compare a php variable to the value of a row in mysql, but keep getting the following error:

 

Wrong perameter count for mysql_result()

 

What syntax would I use to accomplish this?

 

Here is my code:

 

// connects to server and selects database.
include ("../includes/dbconnect.inc.php");

// table name
$table_name = "availability";

// split up date into 3 separate fields
$date = "12/25/2010";
$date_split = explode("/", $date);
$month = $date_split[0];
$day = $date_split[1];
$year = $date_split[2];

// check if earth room is already reserved
$taken = "Reserved";
$sql = "SELECT earth_room FROM $table_name WHERE month='$month' AND day='$day' AND year='$year' LIMIT 1";
$result = mysql_query($sql) or trigger_error("A mysql error has occurred!");
$row = mysql_result($result);

if($row == $taken){
echo "Room Already Reserved";
}
else{
echo "Room Available";
}

 

Thanks for the help,

 

kaiman

Link to comment
https://forums.phpfreaks.com/topic/221701-compare-mysql-row-value-to-php-variable/
Share on other sites

look up the mysql_result function, and you will see that it requires you to specify row too and alternatively a field, not just a mysql query result. ( http://php.net/manual/en/function.mysql-result.php)

You should try to use mysql_fetch_array instead!

You don't actually need to compare any values, you should write your check into your query.

 

$sql = "SELECT earth_room FROM $table_name WHERE month='$month' AND day='$day' AND year='$year' AND earth_room = 'Reserved' LIMIT 1";
if ($result = mysql_query($sql)) {
  if (mysql_num_rows($result)) {
    echo "Room Already Reserved";
  } else {
    echo "Room Available";
  }
} else {
  trigger_error("A useless error message!");
}

@MMDE

 

Ah, yes I see, thanks for pointing that out.

 

@ BK87

 

Thanks mysql_fetch_array worked.

 

@ thorpe

 

Okay, thanks for the alternative method...

 

Sarcasm on the mysql error noted. I tend to use error logs to review problems instead.

 

This is really what the message should say:

trigger_error("A useless error message displayed to hackers who want to learn the directory paths on the server!");

 

Problem solved,

 

kaiman

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.