-1

/MY CODE/

The if part is working properly but else is not working. i even tried $variable instead of direct echo but still it is not working 'else'

Updated

    <?php
   $db = new mysqli('localhost', 'root' ,'', 'timeline');

    if(!$db) {

        echo 'Could not connect to the database.';
    } else {

        if(isset($_POST['queryString'])) {
            $queryString = $db->real_escape_string($_POST['queryString']);

            if(strlen($queryString) >0) {

                $query = $db->query("SELECT collegename FROM college WHERE collegename LIKE '$queryString%' LIMIT 10");
                if(isset($query)) {
                echo '<ul>';
                    while ($result = $query ->fetch_object()) {
                        echo '<li onClick="fill(\''.addslashes($result->collegename).'\');">'.$result->collegename.'</li>';
                    }
                echo '</ul>';

                } else {

                    echo 'create some'; // this part is not working


                }
            } else {
                // do nothing
            }
        } else {
            echo 'There should be no direct access to this script!';
        }
    }
?>

help me out..... even read lots of like problem on stackoverflow but no real return

Aman
  • 806
  • 2
  • 12
  • 38
  • possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – Rizier123 Mar 08 '15 at 11:40
  • You should really say exactly WHAT is actually not working. Your else block is rather huge. – David Losert Mar 08 '15 at 11:57
  • @Charminbear written there which else part is not working -- // ` echo 'create some'; ` // this part is not working – Aman Mar 08 '15 at 11:59
  • First echo $queryString and see data is coming or not – siddhesh Mar 08 '15 at 12:05
  • First echo $queryString and see data is coming or not – siddhesh Mar 08 '15 at 12:09
  • @siddhesh if condition is working properly but only else is not working there.. – Aman Mar 08 '15 at 12:26
  • The if conditions works then never else condition will be exectued that's why it's called as if... else... – siddhesh Mar 08 '15 at 12:35
  • @siddhesh mean to say when query executes itself as the above condition [ IF CONDITION ] then it works but in "else condition" WHEN condition NOT matched --- the else is not working – Aman Mar 08 '15 at 12:38
  • have you tried the dumping of variable $query using var_dump() function? – siddhesh Mar 08 '15 at 12:46

2 Answers2

0

Try

    if($query_run = $db->query("SELECT collegename FROM college WHERE collegename LIKE '$queryString%' LIMIT 10")){

    echo '<ul>';
                  while ($result = $query ->fetch_object()) {
                            echo '<li onClick="fill(\''.addslashes($result->collegename).'\');">'.$result->collegename.'</li>';
                  }

 echo '</ul>';
    } else {
         echo 'create some';
    }
Sandeep
  • 43
  • 1
  • 3
0

If you are using mysqli::query then your if(isset($query)) statement will always be evaluated as true, as $query would be either FALSE or a mysqli_result object. isset returns TRUE for both these values, so your else code will never be called.

Documentation on isset:

Returns TRUE if var exists and has value other than NULL, FALSE otherwise.

Use if($query !== false) instead.

Update

It also seems like you are checking $query to see whether or not there was a hit in the database. You need to check the number of rows in the result for that, e.g:

if ($query !== false && $query->num_rows > 0) {
   // Query was ok and at least one row was returned
}
else {
   // Will be reached if query was bad or there were no hits
}
mhall
  • 3,671
  • 3
  • 23
  • 35