Jump to content

elseif issues


spiceweasel

Recommended Posts

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!!

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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 )

 

 

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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>';
	}

?>

Link to comment
Share on other sites

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()));

}

 

 

Link to comment
Share on other sites

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>';
      }


?>

Link to comment
Share on other sites

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 :)

Link to comment
Share on other sites

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??

 

 

Link to comment
Share on other sites

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()

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.