954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Warning: mysql_fetch_assoc(): 2 is not a valid MySQL result resource

Hi Everyone
I am working through a book called Dreamweaver PHP Web Development. It has a tutorial for creating a booking system for a hotel. The because went out of publication in 2002 and there isn't a errata on the Glasshaus site for it . When creating the search page I am getting the following error message. Warning: mysql_fetch_assoc(): 2 is not a valid MySQL result resource. The page is only showing one result and I think that the error message has something to do with that.

The code for the page is as follows

<?php require_once('Connections/conn1.php'); ?>

<?php
if($_POST['bed_option'] == "less")
{
$bcomp = "<";
}
else if($_POST["bed_option"] == "more")
{
$bComp = ">";
}
else
{
$bComp = "=";
}

?>
<?php
if($_POST["price_option"] == "above")
{
$pComp = ">";
}
else
{
$pComp = "<=";
}
?>
<?php
$bedComp_rsSearch = "<";
if (isset($bComp)) {
$bedComp_rsSearch = (get_magic_quotes_gpc()) ? $bComp : addslashes($bComp);
}
$bedNum_rsSearch = "0";
if (isset($_POST['bed'])) {
$bedNum_rsSearch = (get_magic_quotes_gpc()) ? $_POST['bed'] : addslashes($_POST['bed']);
}
$pComp_rsSearch = "<=";
if (isset($pComp)) {
$pComp_rsSearch = (get_magic_quotes_gpc()) ? $pComp : addslashes($pComp);
}
$pNum_rsSearch = "0";
if (isset($_POST['price'])) {
$pNum_rsSearch = (get_magic_quotes_gpc()) ? $_POST['price'] : addslashes($_POST['price']);
}
mysql_select_db($database_conn1, $conn1);
$query_rsSearch = sprintf("SELECT ID, price, bed, number FROM room WHERE bed %s %s AND price %s %s", $bedComp_rsSearch,$bedNum_rsSearch,$pComp_rsSearch,$pNum_rsSearch);
$rsSearch = mysql_query($query_rsSearch, $conn1) or die(mysql_error());
$row_rsSearch = mysql_fetch_assoc($rsSearch);
$totalRows_rsSearch = mysql_num_rows($rsSearch);
?>

Search for a room
Number of beds :
More thanLess thanEqual to

bed(s)
Price Range :
AboveBelow
$
 


<?php
mysql_free_result($rsSearch);
?>
 
Price
Bed
Room Number

<?php do { ?>
 <?php echo $row_rsSearch['ID']; ?>
<?php echo $row_rsSearch['price']; ?>
 <?php echo $row_rsSearch['bed']; ?>
 <?php echo $row_rsSearch['number']; ?>

<?php } while ($row_rsSearch = mysql_fetch_assoc($rsSearch)); ?>
 



The error message says line 100 which is in the bold part of the code. Hope someone can explain what the error message means and how to fix it.
Thanks Lynne

wattle99
Newbie Poster
2 posts since Aug 2004
Reputation Points: 10
Solved Threads: 0
 

watch for next line ;) before your troubleing code
mysql_free_result($rsSearch);

Cryogenic
Newbie Poster
1 post since May 2005
Reputation Points: 10
Solved Threads: 0
 

<?php do { ?>

I think starting on this line onwards...

rsaicrag
Newbie Poster
5 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

this error mainly results from wrong arguments passed to the mysql_query() function which in turn passes the values to the mysql_fetch_assoc() function,please examine your arguments supplied to mysql_query() especially where you used operators %s =%s and %s=%s

fello
Newbie Poster
1 post since May 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You