Link to home
Start Free TrialLog in
Avatar of Adam
AdamFlag for United Kingdom of Great Britain and Northern Ireland

asked on

Why isn't my 'else' statement working.

Hi all,
I've created a basic search form with 2 inputs from drop down menus which will return results to a results page. My two input fields in the search form are 'language' and 'prefecture'.

My code php below  displays the correct results when there are results in the database. However, when there are no results in the database which match the search criteria,  the code doesn't do what I hoped it would do (i.e. redirect and disconnect). All that happens is that no results are displayed in my results page.  

Any idea what I am doing wrong? Many thanks for reading.

Regards,
Adam

<?php

$language = $_POST['language'];
$prefecture = $_POST['prefecture'];

$sql = "SELECT * FROM teacher_table WHERE language = '".$language."' AND prefecture = '".$prefecture."'";
$result =mysqli_query($db_connection, $sql);

 if($result) { 
    // Results
	While ($rows = mysqli_fetch_array($result)){
	echo $rows['firstname'];
	echo $rows['language'];
	echo $rows['prefecture'];
	}

	
    }
	else {
    // No Results
    redirect_to ('no_results.php');
    db_disconnect($db_connection);
    exit;
    }

?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Adam

ASKER

Ah, I see.

Many thanks Julian for the solution and a clear explanation.

Cheers
You are welcome.