spiceweasel Posted November 26, 2010 Share Posted November 26, 2010 Hi Guys, Beginner needs help with accommodation search feature...i have two drop downs(area and type), each one has an 'any' option. I cant evalute the 'any' options directly against the database so have written this: <?php //query database if ($_SESSION['type']== 'any' && $_SESSION['area']== 'any'){ $query = mysql_query("SELECT * FROM hotels Order by RAND() "); } elseif ($_SESSION['area'] == 'any' && $_SESSION['type'] != 'any'){ $query = mysql_query("SELECT * FROM hotels WHERE type = $_SESSION[type] Order by RAND()"); } elseif ($_SESSION['type'] == 'any' && $_SESSION['area'] != 'any'){ $query = mysql_query("SELECT * FROM hotels WHERE location =$_SESSION[area] Order by RAND()"); } else{ $query = mysql_query("SELECT * FROM hotels WHERE location ='$_SESSION[area]' AND type ='$_SESSION[type]' Order by RAND()"); } //show results while ($search_result = mysql_fetch_assoc($query)) { echo '<b>'.$search_result[name] . '</b><br>'; echo '<p><img src="'.$search_result['photo1'].'" /></p>'; echo '<p><img src="'.$search_result['award'].'" /></p>'; echo $search_result[description] . '<br><br>'; }?> The first if and last else return the correct info from database but the middle two elseif's throw this error: Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given.... I have commented out the queries and replaced them with simple echoes and they all seem to evaluate correctly!. So im lost why $query isnt assigned in the middle two elseif's ?? Any ideas what im doing wrong and is there a better way. Cheers!! Quote Link to comment Share on other sites More sharing options...
intellix Posted November 26, 2010 Share Posted November 26, 2010 Well the two queries are different to the last one. Try wrapping these in single quotation marks as you say the last one works and the middle two do not. // Doesn't work location = $_SESSION[area] type = $_SESSION[type] // Does work location = '$_SESSION[area]' type = '$_SESSION[type]' If you're having issues with SQL you could always add or die (mysql_error()) To see the error that SQL is throwing up (in this case I'm guessing its an SQL syntax error) Quote Link to comment Share on other sites More sharing options...
spiceweasel Posted November 26, 2010 Author Share Posted November 26, 2010 Thanks for the suggestions intellix, Unfortunately i had just forgotten to put the singles back in after trying no quotes and doubles and it makes no difference. Mysql error also returns nothing. Cheers! Quote Link to comment Share on other sites More sharing options...
intellix Posted November 26, 2010 Share Posted November 26, 2010 mmmmm ok could be something else... if you echo the full SQL and put it directly into phpMyAdmin or Workbench or run your SQL query from any console, what happens? does it give you a result set or is it throwing an error and what error is that? echo "SELECT * FROM hotels WHERE type = $_SESSION[type] Order by RAND()"; When I'm having problems like this I put it directly into MySQL Workbench and work with the query until it works and put it back in Quote Link to comment Share on other sites More sharing options...
spiceweasel Posted November 26, 2010 Author Share Posted November 26, 2010 Hi intellix <?php "SELECT * FROM hotels WHERE type = '$_SESSION[type]' Order by RAND()";?> This goes through PHPMyadmin fine if the singles are round $_SESSION[type]. It doesnt return anything as it doesnt recognise $_SESSION[type] but it goes through : MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0024 sec ) Quote Link to comment Share on other sites More sharing options...
mdgeus Posted November 26, 2010 Share Posted November 26, 2010 what type of value is $_SESSION['type'] ? is it an int or string? try it like this if it is a string: "SELECT * FROM hotels WHERE type = '".$_SESSION['type']."' Order by RAND()"; a string needs single quotes around it, an integer doesn't. Have you tried it with the value typed in the query instead of $_SESSION['type'] ? and is that working? Quote Link to comment Share on other sites More sharing options...
spiceweasel Posted November 26, 2010 Author Share Posted November 26, 2010 I thought for a second you had it mdgeus! But nothings changed still throws this error: Warning: mysql_fetch_array() expects parameter 1 to be resource, boolean given in.... Plus the last else statement works without the singles around the string. Code now looks like this: <?php //select correct database $db = mysql_select_db('isleofwightholidays',$connection); if (!$db) { echo 'unable to select database' . mysql_error(); } //query database if ($_SESSION['type']== 'any' && $_SESSION['area']== 'any'){ $query = mysql_query("SELECT * FROM hotels Order by RAND() " or die (mysql_error())); } if ($_SESSION['area'] == 'any' && $_SESSION['type'] != 'any'){ $query = mysql_query("SELECT * FROM hotels WHERE type = ".$_SESSION['type']." Order by RAND()" or die (mysql_error())); } if ($_SESSION['type'] == 'any' && $_SESSION['area'] != 'any'){ $query = mysql_query("SELECT * FROM hotels WHERE location =".$_SESSION['area']." Order by RAND()" or die (mysql_error())); } else{ $query = mysql_query("SELECT * FROM hotels WHERE location ='$_SESSION[area]' AND type ='$_SESSION[type]' Order by RAND()" or die (mysql_error())); } //show results while ($search_result = mysql_fetch_assoc($query)) { echo '<b>'.$search_result[name] . '</b><br>'; echo '<p><img src="'.$search_result['photo1'].'" /></p>'; echo '<p><img src="'.$search_result['award'].'" /></p>'; echo $search_result[description] . '<br><br>'; } ?> Quote Link to comment Share on other sites More sharing options...
mdgeus Posted November 26, 2010 Share Posted November 26, 2010 i think this is the problem: else{ $query = mysql_query("SELECT * FROM hotels WHERE location ='$_SESSION[area]' AND type ='$_SESSION[type]' Order by RAND()" or die (mysql_error())); } You didn't place the quotes right else{ $query = mysql_query("SELECT * FROM hotels WHERE location ='".$_SESSION['area']."' AND type ='".$_SESSION['type']."' Order by RAND()" or die (mysql_error())); } Quote Link to comment Share on other sites More sharing options...
spiceweasel Posted November 26, 2010 Author Share Posted November 26, 2010 Thanks mdgeus I'll check it out but im not sure as the last else is one of the statements that worked. Quote Link to comment Share on other sites More sharing options...
spiceweasel Posted November 26, 2010 Author Share Posted November 26, 2010 Unfortunately none of it works now .....going back to original code: <?php //query database if ($_SESSION['type']== 'any' && $_SESSION['area']== 'any'){ $query = mysql_query("SELECT * FROM hotels Order by RAND() "); } elseif ($_SESSION['area'] == 'any' && $_SESSION['type'] != 'any'){ $query = mysql_query("SELECT * FROM hotels WHERE type = $_SESSION[type] Order by RAND()"); } elseif ($_SESSION['type'] == 'any' && $_SESSION['area'] != 'any'){ $query = mysql_query("SELECT * FROM hotels WHERE location =$_SESSION[area] Order by RAND()"); } else{ $query = mysql_query("SELECT * FROM hotels WHERE location ='$_SESSION[area]' AND type ='$_SESSION[type]' Order by RAND()"); } //show results while ($search_result = mysql_fetch_assoc($query)) { echo '<b>'.$search_result[name] . '</b><br>'; echo '<p><img src="'.$search_result['photo1'].'" /></p>'; echo '<p><img src="'.$search_result['award'].'" /></p>'; echo $search_result[description] . '<br><br>'; } ?> Quote Link to comment Share on other sites More sharing options...
intellix Posted November 26, 2010 Share Posted November 26, 2010 Hi intellix <?php "SELECT * FROM hotels WHERE type = '$_SESSION[type]' Order by RAND()";?> This goes through PHPMyadmin fine if the singles are round $_SESSION[type]. It doesnt return anything as it doesnt recognise $_SESSION[type] but it goes through : MySQL returned an empty result set (i.e. zero rows). ( Query took 0.0024 sec ) I meant once you've run it through PHP to see what exactly its doing.. You need to know exactly what $_SESSION['type'] is, it might be nothing, a number, string, something you aren't expecting // It could print SELECT * FROM hotels WHERE type = '' Order by RAND() // Or maybe this SELECT * FROM hotels WHERE type = '2' Order by RAND() // Or this SELECT * FROM hotels WHERE type = 'two' Order by RAND() Just wanted to know what SQL said about the end result Quote Link to comment Share on other sites More sharing options...
spiceweasel Posted November 26, 2010 Author Share Posted November 26, 2010 Hi intellix, I think i see what you mean. If i take out the query and just echo back the session value for each if statement it appears to be a string with the expected value eg. any any, hotel any, cornwall any ....etc - so it appear to be working. As a further check I have run each query on its own and they work independently and return the correct data just not within the if/ifelse's I cant understand why not. Im getting really confused and might start to look for an alternative way of doing this maybe a switch?? Quote Link to comment Share on other sites More sharing options...
waterssaz Posted November 26, 2010 Share Posted November 26, 2010 You say you can't evaluate 'any' against the database which is why you have so many queries going on which is overcomplicating the code in my view. Why don't you evaluate the $_SESSION['type' and $_SESSION['area'] 1st to seewhat they contain like so: if ($_SESSION['area']== 'any') { $area = '%'; } else{ $area = $_SESSION['area']; } if ($_SESSION['type']== 'any') { $type = '%'; } else{ $type = $_SESSION['type']; } Then run your query inserting those variables but using like() Quote Link to comment Share on other sites More sharing options...
spiceweasel Posted November 26, 2010 Author Share Posted November 26, 2010 YES!!!!!!!!!!!!!!!!!! thank you waterssaz that works - as a beginner i dont quite understand why, but it works and thats good enough for me. Thank you to everyone that helped. Quote Link to comment Share on other sites More sharing options...
intellix Posted November 26, 2010 Share Posted November 26, 2010 For performance reasons (probably not needed in this case?) I wouldn't have used like when not needed Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.