0

I got my search page error and the server pop up below message:

Notice: Undefined variable: query in /home/tz005/public_html/COMP1687/search.php on line 64 Minimum length is 3

Where should I make a correction in the script and how to define query? Here is my php script:

<?php

$min_length = 3; //min length of the search

if(strlen($query) >= $min_length){

        $query = htmlspecialchars($query); 

        $raw_results = mysqli_query($GLOBALS["___mysqli_ston"], "SELECT * FROM item_information
            WHERE (`itemtitle` LIKE '%".$query."%')") or die(((is_object($GLOBALS["___mysqli_ston"])) ? mysqli_error($GLOBALS["___mysqli_ston"]) : (($___mysqli_res = mysqli_connect_error()) ? $___mysqli_res : false)));


        if(mysqli_num_rows($raw_results) > 0){ // If it find's more than 0 results...

            while($results = mysqli_fetch_array($raw_results)){

echo "<p>".$results['itemtitle']."</p>"; // show's the results..


            }

        }
        else{ // If found nothing..
            echo "No results";
        }

    }
    else{ // if length of the search is less than defined on variable...
        echo "Minimum length is ".$min_length;
    }
?>
AeJey
  • 1,447
  • 20
  • 40

1 Answers1

1

Change the line if(strlen($query) >= $min_length){ to if(isset($query) && strlen($query) >= $min_length){.

Klemen Tusar
  • 9,261
  • 4
  • 31
  • 28
  • Or use `!empty()` rather than `isset()`, because when somewhere in code is `$query = $_GET['query']` and URL will be like `file.php?query=`, it will be same problem again. – Patrik Krehák May 22 '14 at 07:06
  • Why?! The length of `$query` would not be >= to `$min_length` anyway. – Klemen Tusar May 22 '14 at 07:07
  • @techouse that's why: `isset("") == true;` but `!empty("") == false;`. Both of them works fine (in this script), but this is litle more `logistic` :) There is more about it: http://techtalk.virendrachandak.com/php-isset-vs-empty-vs-is_null/ – Patrik Krehák May 22 '14 at 07:10
  • Holly sh*t. Didn't know `isset()` was broken. I presumed it worked like `defined` in Perl, but after reading this http://stackoverflow.com/questions/418066/best-way-to-test-for-a-variables-existence-in-php-isset-is-clearly-broken well... yea... another PHP fail :P – Klemen Tusar May 22 '14 at 07:15