AJAX database search script

Here is the AJAX piosk script without reloading the page. Why does it work fine in Opera, but in other browsers it gives an error when entering some characters.

Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in Z: \home\localhost\www\getuser.php on line 12

Here is the script itself.

getuser.php:

<?php
mysql_connect("localhost", "zip", "123123") or die("   ");
mysql_select_db("zip") or die("  ");
mysql_set_charset("utf8");
header("Content-Type: text/html;charset=UTF-8;");
$user_get = $_GET['q'];
iconv("UTF-8", "WINDOWS-1251", $user_get);
//echo $user_get;
if ((isset($user_get))) {
    $sql = mysql_query("SELECT * FROM `user` WHERE `lastname` LIKE '%{$user_get}%'");
    if (mysql_num_rows($sql) != 0) {
        while ($row = mysql_fetch_assoc($sql)) {
            echo $row['lastname'] . "&nbsp;" . $row['name'] . "<br>";
        }
    } else {
        echo " ";
    }
} else {
    echo " ";
}
?>

ajax.php:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8;">
<script type="text/javascript">
function showHint(str) {
    if (str.length == 0) {
        document.getElementById("test").innerHTML = "";
        return;
    }
    if (window.XMLHttpRequest) { //   IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp = new XMLHttpRequest();
    } else { //   for IE6, IE5
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.onreadystatechange = function () {
        if (xmlhttp.readyState == 4) //     ..
        {
            if (xmlhttp.status == 200) //    200  
            {
                //    ID="test"
                document.getElementById("test").innerHTML = xmlhttp.responseText; 
            } else //    200 
            {
                //  
                alert('  '); 
            }
        } else //      
        {
            //    ID="test"
            document.getElementById("test").innerHTML = '...';
        }
    }
    //   GET  getuser.php
    xmlhttp.open("GET", "getuser.php?q=" + str, true);
    xmlhttp.send();
}
</script>
</head>
<body>
<p><b>    :</b></p>
<form>
<input type="text" onkeyup="showHint(this.value)" size="20" />
</form>
<p><span id="test"></span></p>
</body>
</html>

Answer 1, authority 100%

  1. What characters cause the error?
  2. Offhand – transform the parameters passed through the URL like this:
    xmlhttp.open(“GET”,”getuser.php?q=”+encodeURIComponent(str),true);